Reputation: 400
I have an SQL query that populates a DataGridView through a DataTable:
SELECT top 100 a.Ordnum, oldBFE, newBFE, oldCBRA, newCBRA, oldLOMC, newLOMC, oldfld, newfld FROM
(SELECT Ordnum, PrpBFE as oldBFE, CBRADte as oldCBRA, LOMCDte as oldLOMC, FldZne AS oldfld
FROM [TOD].[dbo].[Orders] with (NOLOCK) WHERE RecRevDesc = '1 - Order Clone') a
JOIN
(SELECT Ordnum, PrpBFE as newBFE CBRADte as newCBRA, LOMCDte as newLOMC, FldZne AS newfld
FROM [TOD].[dbo].[Orders] with (NOLOCK) WHERE RecRevDesc = '2 Determination Completed-Workflow') b
ON a.Ordnum = b.Ordnum
If the value of certain pairs of cells are not equal, I need both cells to have a red forecolor. Right now, I am firing this through the cellformatting event:
For i As Integer = 0 To Me.gridCompare.RowCount - 1
If Me.gridCompare.Rows(i).Cells(1).ToString <> Me.gridCompare.Rows(i).Cells(2).ToString Then
Me.gridCompare.Rows(i).Cells(1).Style.ForeColor = Color.Red
Me.gridCompare.Rows(i).Cells(2).Style.ForeColor = Color.Red
ElseIf Me.gridCompare.Rows(i).Cells(3).ToString <> Me.gridCompare.Rows(i).Cells(4).ToString Then
Me.gridCompare.Rows(i).Cells(3).Style.ForeColor = Color.Red
Me.gridCompare.Rows(i).Cells(4).Style.ForeColor = Color.Red
ElseIf Me.gridCompare.Rows(i).Cells(5).ToString <> Me.gridCompare.Rows(i).Cells(6).ToString Then
Me.gridCompare.Rows(i).Cells(5).Style.ForeColor = Color.Red
Me.gridCompare.Rows(i).Cells(6).Style.ForeColor = Color.Red
ElseIf Me.gridCompare.Rows(i).Cells(7).ToString <> Me.gridCompare.Rows(i).Cells(8).ToString Then
Me.gridCompare.Rows(i).Cells(7).Style.ForeColor = Color.Red
Me.gridCompare.Rows(i).Cells(8).Style.ForeColor = Color.Red
End If
Next
However, only cells 1 and 2 have the correct formatting. What am I doing wrong? The order of the columns does not matter. I have tried individual IF statements as well.
Upvotes: 1
Views: 460
Reputation: 81610
You can't use the ElseIf branch since that will prevent testing the other pairs. Put each test in it's own IF-EndIf. You also need to test the Value property of the Cell:
If Me.gridCompare.Rows(i).Cells(1).Value.ToString <> _
Me.gridCompare.Rows(i).Cells(2).Value.ToString Then
'etc
End If
If Me.gridCompare.Rows(i).Cells(3).Value.ToString <> _
Me.gridCompare.Rows(i).Cells(4).Value.ToString Then
'etc
End If
Also, make sure those cells aren't null (or nothing) or else it will throw an exception.
Upvotes: 1