Search code examples
vb.netdatagridviewverification

Last row verification in DataGridView


I have this body of code that verifies rows in a DataGridView. It checks if the content of a cell in that row is between 0 to 10 and if it is 11+, it would be highlighted Red.

The issue that I have come across is that even if all numbers are within 0-10, the last row in the row would be highlighted Red. Is this some sort of end of file situation?

In the code, if the cell is null, nothing happens. So I figured the last row should have nothing done to it as well (as long as it is 0-10)

For Each row As DataGridViewRow In DataGridView1.Rows
    If Not row.Cells("F3").Value Is DBNull.Value Then
        Dim cellNumber As Integer

        If Integer.TryParse(row.Cells("F3").Value, cellNumber) AndAlso cellNumber >= 0 AndAlso cellNumber <= 10 Then
            'All pass verification, Do nothing
        Else
            'Point out the wrong value 
            row.Cells("F3").Style.BackColor = Color.Red
        End If
    Else
        '    MessageBox.Show("Not a number!")
    End If
Next

Solution

  • I figured out the line that I was missing .. I had to add:

    AndAlso Not row.Cells("F11").value Is Nothing

    to have :

    If Not row.Cells("F3").Value Is DBNull.Value AndAlso Not row.Cells("F11").value is Nothing Then