Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, How can i change the color of the specific rows that contains "SOLD"

Here's my code

C#
string a = dataGridView1.Rows[0].Cells[2].Value.ToString();
                foreach (DataGridViewRow Myrow in dataGridView1.Rows)
                {
                    if (a == "Luzon")//"))//Columns.Contains("SOLD"))//Columns.Contains("SOLD"))
                    {
                        dataGridView1.DefaultCellStyle.BackColor = Color.Red;
                      
                    }
                }


After doing using this code, all rows turns red, What i want is to change the color of the rows that has the word "SOLD" . Please help
Posted
Comments
phil.o 18-Dec-15 6:24am    
Is this WPF? If so, you can use a converter for the BackColor property implementing this logic.

1 solution

Currently you are setting the color for the DataGridView to red.

If you just want a specific row you have to set the color for this row.
Just like this:

C#
myrow.defaultcellstyle.forecolor="Color.Red";


BTW. just to improve your code a bit, why do you set a outside of the loop?

This would be better i guess

C#
string a = dataGridView1.Rows[0].Cells[2].Value.ToString();
                foreach (DataGridViewRow Myrow in dataGridView1.Rows)
                {
                    if (Myrow.Cells[2].Value.ToString() == "Luzon")//"))//Columns.Contains("SOLD"))//Columns.Contains("SOLD"))
                    {
                        Myrow.DefaultCellStyle.BackColor = Color.Red;                      
                    }
                }


Even better would be to use to Columns name instead of the 2 in this :)
like Myrow.Cells["NameOfColumn"].Value
 
Share this answer
 
v4
Comments
Rencyrence 18-Dec-15 19:37pm    
Hello, i think this doesnt work it gives me error Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.DataGridViewRow'
Rencyrence 28-Dec-15 1:01am    
Please help, anyone?
HobbyProggy 19-Jan-16 4:44am    
Sorry i respond so late, i didn't see you replied.
I messed it up with the usual GridRow i updated the solution

It is Myrow.Cells[2].Value.ToString() == "";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900