-1

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

Dave B
  • 659
  • 8
  • 29
  • 2
    If IsDBNull(CellValue) OrElse CellValue Is Nothing OrElse ... – Aaron Feb 22 '17 at 17:41
  • 1
    You need to reorder your checks so that `CellValue is Nothing` comes first... You can't check its `ToString() ` method if it's null. You also need to loop backwards instead of forward, otherwise once you delete a row, your index value will be out of sync. If you must go forward, you'll have to adjust the value of `i` manually after you delete – pinkfloydx33 Feb 22 '17 at 17:42

1 Answers1

2

You want two different things:

  1. The null reference exception is resolvable by reordering the conditions in your If command.
  2. The need to rerun the removal multiple times is due to how you iterate over the rows. Your For loop iterates up to the number of rows in your grid view. Let's say you currently look at row 4 and decide to delete it. Now the row with former index 5 takes that position (the index of every following row is reduced by one). In the next iteration you look at index 5 which is now the row that has had index 6, which means you will never look at the original fifth row. To resolve this reverse your loop.

Try this slightly changed version of your code (untested):

Private Sub btnRemoveBlanks_ItemClick(sender As Object, e As ItemClickEventArgs) Handles btnRemoveBlanks.ItemClick
    Try
        Dim rowIdx as Integer = StudentsGrid.DataRowCount - 1
        For i as Integer = rowIdx To 0 Step -1
            Dim CellValue As Object = StudentsGrid.GetRowCellValue(I, "FirstName")
            If CellValue Is Nothing OrElse IsDBNull(CellValue) OrElse String.IsNullOrWhiteSpace(CellValue.ToString()) 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
Georg Jung
  • 949
  • 10
  • 27