c# DataGridView change row backcolour if pervious value is not a match to current

Gerald Oakham 101 Reputation points
2022-08-12T09:58:00.357+00:00

Hi, I'm trying to format a DataGridView so that if the Previous Cell value doesn't match the current one, then the current row's background colour is changed.
If they do match, then I need to background colour to match the colour it was previously changed to.

I found out about checking if a value is odd or even, which kind of put me in the right direction (https://csharp-station.com/how-to-test-for-even-or-odd-numbers-in-c/)

So my grid initially looks like this :
230761-dgvnf.gif

and when I run my code

        private void button1_Click(object sender, EventArgs e)   
        {              
dataGridView3.DefaultCellStyle.BackColor = Color.White;  
  
            int Counter6 = 0;  
            int seek3 = 0;  
            int perviousValue = 1;  
            int nomatch = 0;  
  
            foreach (DataGridViewRow item5 in dataGridView3.Rows)  
            {  
                seek3 = Convert.ToInt32(item5.Cells["posidDataGridViewTextBoxColumn"].Value);  
  
                if (seek3 == perviousValue)  
                {  
                    Counter6++;  
                }  
                else  
                {  
                    perviousValue = seek3;  
  
                    if (nomatch % 2 == 0)  
                    {  
                        dataGridView3.Rows[Counter6].DefaultCellStyle.BackColor = Color.LightSalmon;  
                    }  
                    else  
                    {  
                        dataGridView3.Rows[Counter6].DefaultCellStyle.BackColor = Color.White;  
  
                    }  
                    Counter6++;  
                    nomatch++;  
                }  
  
               
            }  
        }  

it kind of works
230628-dgvwf.gif

but the "11" 's (All three) should be pink, as shul the 13's. 14'th are ok.

Can any one advise where eI am going wrong, and what to do to resolve it. ?

thanks

Developer technologies .NET Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Gerald Oakham 101 Reputation points
    2022-08-15T07:22:43.257+00:00

    For anyone following this, and for Future me looking for the solution again ( :-) ), it might not be the prettiest of code, but it (seems to) work.

            private void button1_Click(object sender, EventArgs e)   
            {  
      
                dataGridView3.DefaultCellStyle.BackColor = Color.White;  
      
                int Counter6 = 0;  
      
                //ADDED TO SOLVE ISSUE  
                int previousCounter6 = Counter6-1;  
                bool isPink = false;  
                //ADDED TO SOLVE ISSUE  
      
                int seek3 = 0;  
                int perviousValue = 1;  
                int nomatch = 0;  
      
                foreach (DataGridViewRow item5 in dataGridView3.Rows)  
                {  
                    seek3 = Convert.ToInt32(item5.Cells["posidDataGridViewTextBoxColumn"].Value);  
      
                    if (seek3 == perviousValue)  
                    {  
                        //ADDED TO SOLVE ISSUE  
                        if (isPink == true)  
                        {  
                            if (previousCounter6 == -1)  
                            {  
                                previousCounter6 = 0;  
                            }  
                        //ADDED TO SOLVE ISSUE  
                            dataGridView3.Rows[previousCounter6].DefaultCellStyle.BackColor = Color.LightSalmon;  
                        }  
                        else  
                        {  
                            //ADDED TO SOLVE ISSUE  
                            if (previousCounter6 == -1)  
                            {  
                                previousCounter6 = 0;  
                            }  
                            //ADDED TO SOLVE ISSUE  
      
                            dataGridView3.Rows[previousCounter6].DefaultCellStyle.BackColor = Color.White;  
      
                        }  
      
                        Counter6++;  
                        //ADDED TO SOLVE ISSUE  
                        previousCounter6++;  
                        //ADDED TO SOLVE ISSUE  
                    }  
                    else  
                    {  
                        perviousValue = seek3;  
      
                        if (nomatch % 2 == 0)  
                        {  
                            dataGridView3.Rows[Counter6].DefaultCellStyle.BackColor = Color.LightSalmon;  
                            //ADDED TO SOLVE ISSUE  
                            isPink = true;  
                            //ADDED TO SOLVE ISSUE  
                        }  
                        else  
                        {  
                            dataGridView3.Rows[Counter6].DefaultCellStyle.BackColor = Color.White;  
                            //ADDED TO SOLVE ISSUE  
                            isPink = false;  
                            //ADDED TO SOLVE ISSUE  
                        }  
                        Counter6++;  
                        //ADDED TO SOLVE ISSUE  
                        previousCounter6++;  
                        //ADDED TO SOLVE ISSUE  
                        nomatch++;  
                    }  
      
                   
                }  
            }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.