How to change row color in datagridview?

EB. picture EB. · Feb 3, 2010 · Viewed 425.5k times · Source

I would like to change the color of a particular row in my datagridview. The row should be changed to red when the value of columncell 7 is less than the value in columncell 10. Any suggestions on how to accomplish this?

Answer

Ricardo Sanchez picture Ricardo Sanchez · Feb 3, 2010

You need to loop through the rows in the datagridview and then compare values of columns 7 and 10 on each row.

Try this:

foreach (DataGridViewRow row in vendorsDataGridView.Rows) 
     if (Convert.ToInt32(row.Cells[7].Value) < Convert.ToInt32(row.Cells[10].Value)) 
     {
         row.DefaultCellStyle.BackColor = Color.Red; 
     }