0

I am just wanting to change the colour of a row if it has a zero in it but I keep getting this error:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
     string comp = "0";
     if (row.Cells[4].Value.ToString() == comp)
     {
         row.DefaultCellStyle.ForeColor = Color.Red;
     }

     if (row.Cells[4].Value.ToString() == "1")
     {
         row.DefaultCellStyle.ForeColor = Color.Green;
     }
}

Also, I cannot get my code to display in this (stackoverflow) window without assistance. How can I do this?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Greg Adler
  • 99
  • 8
  • `NullReferenceException` is a common situation for beginner programmers. The link provided should help you understand the problem. Then use the debugger to find what/where/when you have a variable that is `null`. – Soner Gönül Jul 14 '15 at 08:17
  • Check if you have 5 cells in grid? use row.Cells[3] if you have four cells, as it is zero based index – Adil Jul 14 '15 at 08:17
  • `row.Cells[4].Value` may be null. – NASSER Jul 14 '15 at 08:17

0 Answers0