Simple error?
I have a DataGridView that is displaying entries from a sreadsheet. I was to be able to change the background colours of individual cells based on what is contained in those cells. I have used the code below stays red even though some should be green!
Can anyone see why?
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells("OnTrip").Value = "NOT YET" Then
DataGridView1.Columns.Item(11).DefaultCellStyle.BackColor = Color.Red
Else
If row.Cells("OnTrip").Value = "YES!!" Then
DataGridView1.Columns.Item(11).DefaultCellStyle.BackColor = Color.Green
End If
End If
Next
Thanks in advance! ;)
# 1 Re: Simple error?
What I'd do here WelshWarrior, is to set a breakpoint at this line :
If row.Cells("OnTrip").Value = "NOT YET" Then
Then run it. When the debugger encounters the line containing the Breakpoint, it will enter Break Mode ( IOW. the program will pause, and you will be returned to the code view ). Now, while you're in Break Mode, you can establish the values of all your variables ( make sure your Locals Window is displayed, by saying Debug, Windows, Locals ), then also make sure your Debug toolbar is present, you can make it appear by right clicking on any toolbar, then selecting Debug from the list.
Now, once all these "debugging tools" are in place, you can step through your code, by selecting the Step Into button from the Debug toolbar, or by pressing F8.
The whole purpose of this is to determine what values are where, at a guess, I'd say that somewhere in your particlar datagridview cell, there is wrong data, and that may be the reason why the cells don't turn green.
# 3 Re: Simple error?
Found the error. Here's what the code should read:
If row.Cells("OnTrip").Value = "YES" Then
row.Cells(11).Style.BackColor = Color.Green
Else
If row.Cells("OnTrip").Value = "NOT YET" Then
row.Cells(11).Style.BackColor = Color.Red
End If
End If
Thanks again for your help