datagridview background color changing on column sort

compguru910

Well-known member
Joined
Nov 25, 2009
Messages
47
Programming Experience
3-5
Ok, when I populate my datagridview, after the rows are filled, I run a For Next loop detecting if the date field is greater than Now, if so, it changes the background color of that row. I noticed that if I sort the field, it changes the background color back to white. So, I tried just calling the code that changes the color the fist time, but I get this error

operator '<' not defined for type DBNull and Date Type

Im guessing this has something to do with the call to the datagridview row index, since it changes when you click the column header. Im kinda stuck with a brainfart trying to figure out how to make it work. Here is the code

VB.NET:
   Public Sub ColorRows()

        For i = 0 To dgvMain.Rows.Count - 1
            Select Case dgvMain.Item(7, i).Value
                Case Is < Now
                    dgvMain.Rows(i).DefaultCellStyle.BackColor = Color.IndianRed
                Case Is > Now
                    dgvMain.Rows(i).DefaultCellStyle.BackColor = Color.CornflowerBlue
                Case Is = Now
                    dgvMain.Rows(i).DefaultCellStyle.BackColor = Color.Green
            End Select

        Next
    End Sub

Thanks again in advanced
 
Nevermind, I fixed it. It seems it didnt like my Select Case statement. I switched it out for an If Then statement, and it works fine. But, I would like to know why it doesnt work with the select case statement.
 
Can you post the working code? It should work with the Select Case syntax.

The error means that the '<' operator is not defined when comparing a date and a null value. Since we know 'Now()' will give us a date the other object in the comparison is a null value. That means in one of the cells that you check there is no data present.

I may be wrong but the idea is to iterate over each row of the DataGrid and compare the data that is in the 6th column to the current date. But you are comparing every column in the 6th row to the current date.

I believe
VB.NET:
Select Case dgvMain.Item(7, i).Value

Should Be
VB.NET:
Select Case dgvMain.Item(i, 7).Value
 
That part of the code was fine. It was the fact that when you change the sorting of your columns, the datagridview doesnt change the row index of the rows, so that will just give you an error. I had to switch it to a For Each loop. Here is the correct code.

VB.NET:
    Public Sub ColorRows()

        For Each row As DataGridViewRow In dgvMain.Rows
            If row.Cells(7).Value > Now Then
                row.DefaultCellStyle.BackColor = Color.Aqua
            End If

            If row.Cells(7).Value < Now Then
                row.DefaultCellStyle.BackColor = Color.Red
            End If
        Next

    End Sub
 
{Be hAppy}

Change code as below:

Public Sub ColorRows()

For i = 0 To dgvMain.Rows.Count - 1
Select Case dgvMain.Item(7, i).Value
Case Is DBNull
dgvMain.Rows(i).DefaultCellStyle.BackColor = Color.Red
Case Is < Now
dgvMain.Rows(i).DefaultCellStyle.BackColor = Color.IndianRed
Case Is > Now
dgvMain.Rows(i).DefaultCellStyle.BackColor = Color.CornflowerBlue
Case Is = Now
dgvMain.Rows(i).DefaultCellStyle.BackColor = Color.Green
End Select

Next
End Sub
 
Back
Top