I have the following code which removes blank rows from a Devexpress Datagrid.
 Private Sub btnRemoveBlanks_ItemClick(sender As Object, e As ItemClickEventArgs) Handles btnRemoveBlanks.ItemClick
    Try
    Dim I As Integer
        For I = 0 To StudentsGrid.DataRowCount - 1
            Dim CellValue As Object = StudentsGrid.GetRowCellValue(I, "FirstName")
        If String.IsNullOrWhiteSpace(CellValue.ToString) = True OrElse CellValue Is Nothing OrElse CellValue Is DBNull.Value Then
            ' Debug.Print("Empty")
            StudentsGrid.DeleteRow(I)
        Else
            ' Debug.Print("Not Empty")
        End If
        Next
    StudentsGrid.RefreshData()
    btnRefresh.PerformClick()
     Catch ex As Exception
     MessageBox.Show(ex.Message, "Error: " & System.Reflection.MethodBase.GetCurrentMethod.Name, MessageBoxButtons.OK, MessageBoxIcon.Error)
     End Try
End Sub
However, one problem is that I am receiving
Object reference not set to an instance of an object.
on the line starting: if string.isnullorwhitespace
Or I have to click the button two or three times to remove all the blanks.
Is there a better way to determine if the row is empty or whitespace? Any ideas how I might improve this code?
Edit: The primary reason for the question was to remove the blanks, which I was able to do by stepping backwards through the grid
 
    