Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

cell fromtating of DataGridView need to compare two values from two cells same row, c#

Posted on 2011-09-28
2
Medium Priority
?
1,704 Views
Last Modified: 2012-05-12
I have a win form that has a gridview, and I need to change the color of the cell, based on itslef and a different cell.
If you look at the code below it works fine when changign the color of the cell besad on the current cells value.
But I need to be able to compare both values and then change the cell color.

The position of the colums can change but not the name.

        private void ECCN_CellFormatting(DataGridViewCellFormattingEventArgs ee, string case_str)
        {

            switch (case_str)
            {
                case "Y": ee.CellStyle.BackColor = Color.LawnGreen; break;
                case "N": ee.CellStyle.BackColor = Color.Red; break;
                case "0": ee.CellStyle.BackColor = Color.Red; ee.CellStyle.ForeColor = Color.Yellow; break;
                default: break;
            }
        }

        private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs ee)
        {

            switch (dataGridView1.Columns[ee.ColumnIndex].Name)
            {
                case "eccn_available": ECCN_CellFormatting(ee, (string)ee.Value); break;
                case "eccn_value": ECCN_CellFormatting(ee, (string)ee.Value); break;
                default: break;
            }
        }


these methods are called by :
        public void InitializeGrid(DataGridView lv_StockCheckdataGridView
                                  ,string GridViewName)
        {

 
            lv_StockCheckdataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            lv_StockCheckdataGridView.ContextMenuStrip = this.contextMenuStrip1;
            lv_StockCheckdataGridView.AllowUserToAddRows = false;
            lv_StockCheckdataGridView.AllowUserToDeleteRows = false;
            lv_StockCheckdataGridView.AllowUserToOrderColumns = true;
            lv_StockCheckdataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            lv_StockCheckdataGridView.Size = new System.Drawing.Size(1074, 604);
            lv_StockCheckdataGridView.TabIndex = 5;
            lv_StockCheckdataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGridView1_MouseDown);
            lv_StockCheckdataGridView.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dataGridView_CellFormatting);
            lv_StockCheckdataGridView.Location = new System.Drawing.Point(12, 43);
            lv_StockCheckdataGridView.Name = GridViewName;
            lv_StockCheckdataGridView.ReadOnly = true;
            lv_StockCheckdataGridView.AllowUserToResizeColumns = true;

        }


I need a way to look at all the values for the rows I have just populated in the grid and
0
Comment
Question by:jhacharya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 2000 total points
ID: 36812654
Hi,

Each row in the grid has cells to hold the data. So u can verify the cells in the row which u want before calling the actual Formatting as shown below.

Here i am checking the values in the current cell with the first cell in my row when they are equal then i call the method in the switch.

 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs ee)
{
if (ee.RowIndex > -1)
{
                DataGridViewRow row = dataGridView1.Rows[ee.RowIndex];

                switch (dataGridView1.Columns[ee.ColumnIndex].Name)
                {
                    case "eccn_available":

                        if (Convert.ToString(ee.Value).Equals(Convert.ToString(row.Cells[0].Value)))
                        {
                            ECCN_CellFormatting(ee, (string)ee.Value);
                        }
                        break;
                    case "eccn_value":
                        ECCN_CellFormatting(ee, (string)ee.Value);
                        break;
                    default: break;
                }
            }
}
0
 
LVL 2

Author Closing Comment

by:jhacharya
ID: 36962867
I have been pulled away from this project at the moment, so I can confirm the result from the solution, but I am going to give you the points and assume for now that the solution works, thanks Jay
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question