I need to change the colors of a data grid view rows according to the data in the rows.
My code is:
foreach (DataGridViewRow Myrow in datagrid1.Rows)
{
if (Myrow.Cells[0].Value.Equals("Red"))
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
}
But when I try to run it I get: Object reference not set to an instance of an object
I assume it's because it keeps looping every row until it reaches some which has a null value. How do I stop it from doing that?