-1

I used this code to change rows' color with a condition but the code doesn't work! I don't know why, I have an error :

An unhandled exception of type System.NullReferenceException' occurred in db(name of the project).exe

when I arrive at "color= dataGridView1.Rows...."

What should I do?

while (true)
{
    color = dataGridView1[2, rowindex].Value.ToString();
    if (color == "IDLE")
    {
        dataGridView1.Rows[rowindex].DefaultCellStyle.BackColor = Color.Orange;
    }
    if (color == "ACTIVE")
    { dataGridView1.Rows[rowindex].DefaultCellStyle.BackColor = Color.Green; }
    if (color == "MAINTENANCE")
    { dataGridView1.Rows[rowindex].DefaultCellStyle.BackColor = Color.Purple; }
    if (color == "DISMISSED")
    { dataGridView1.Rows[rowindex].DefaultCellStyle.BackColor = Color.Red; }
    if (color == null)
    { break; }
    rowindex++;
}
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
  • `What should i do?` debug it: go line by line and find the source of problem – ASh Feb 16 '16 at 19:42
  • use the debugger..perhaps you should use a foreach loop or a proper for loop to loop through the number of rows also when are you setting the value to false to jump out of the while loop.. – MethodMan Feb 16 '16 at 19:47
  • you should be using a foreach loop like this then check the values in the row for example `foreach (DataGridViewRow row in dataGridView1.Rows)` assuming the name of the column is `color` if so I will post a refactored answer for you to test – MethodMan Feb 16 '16 at 19:48

1 Answers1

0
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (row.Cells["color"] == "IDLE")
    {
        row.DefaultCellStyle.BackColor = Color.Orange;
    }
    else
    if (row.Cells["color"] == "ACTIVE")
    { 
        row.DefaultCellStyle.BackColor = Color.Green; 
    }
    else
    if (row.Cells["color"] == "MAINTENANCE")
    { 
        row.DefaultCellStyle.BackColor = Color.Purple; 
    }
    else
    if (row.Cells["color"] == "DISMISSED")
    { 
        row.DefaultCellStyle.BackColor = Color.Red; 
    }
    //else
    //{ 
    //    break; //probably don't need this since the foreach will know when it's done looping through all the Rows
    //}
}
MethodMan
  • 17,665
  • 6
  • 32
  • 52
  • should I put the column name in the square brackets? – davidemarchiori Feb 16 '16 at 20:55
  • yes the Column name needs to be in where I have `["color"]` replace it with the correct column name – MethodMan Feb 16 '16 at 20:57
  • the program says that he can't find the column... should i put something different in the place of "row" in the round brackets? – davidemarchiori Feb 16 '16 at 21:12
  • use the debugger and when you get to row using the QuickWatch, what are the columns array values..? you need to name it the column name of the DataGridView like you have in the aspx – MethodMan Feb 16 '16 at 21:28
  • When you post an answer, it's helpful to provide an explanation of what was wrong, rather than just a code dump. – mason Feb 20 '16 at 02:06