Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I wanna put any color in my datagrid view in a row that is < 5

I tryied something like that but it doesnt work

C#
if (siDataSet.vine.MinimalColumn <= 5)
           {
           this.vineDataGridView.Rows[RowIndex].DefaultCellStyle.BackColor = Color.Red;


}
any idea ?
Posted
Comments
Ryszard50 10-Jun-13 18:55pm    
I forgot to write, I wanna take just values from one column.

If you are trying to point out there are less than 5 columns,
Try to itterate through the rows and cells of the datagridview,
C#
foreach (DataGridViewRow r in dataGridView1.Rows)
           {
               if (r.Cells.Count <= 5)
               {
                   foreach (DataGridViewCell c in r.Cells)
                   {
                       if (c.OwningColumn.HeaderText == "The text of the header")
                       {
                           c.OwningColumn.DefaultCellStyle.BackColor = Color.Red;
                       }
                   }
               }
           }
 
Share this answer
 
Comments
Ryszard50 11-Jun-13 13:26pm    
sorry I didn't explain propely... it doesn't work propely it highlights all collumn insted of the cell in the column with the value less than 5...
 
Share this answer
 
If you are sure the value of the cell is a number
you can try this.
C#
DataGridViewCellStyle cs = new DataGridViewCellStyle();
           cs.BackColor = Color.LightBlue;
           foreach (DataGridViewRow r in dataGridView1.Rows)
           {
                   foreach (DataGridViewCell c in r.Cells)
                   {
                       int i = int.Parse(c.Value.ToString());
                       if (i <= 6)
                       {
                           c.Style = cs;
                       }
                   }
               }
           }
 
Share this answer
 
Comments
Ryszard50 11-Jun-13 17:14pm    
Thank you for you help, I solved the problem... I had to modifie the code but it works.
Ryszard50 11-Jun-13 17:15pm    
Thank you for you help, I solved the problem...

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