-1
我有以下代码,它从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
然而,一个问题是,我接收
对象引用不设置为一个对象的一个实例。
上线起点:if string.isnullorwhitespace
或者我必须点击该按钮两次或三次,除去所有的空白。
有没有更好的方法来确定行是空的还是空白的?任何想法我可能会改进这个代码?
编辑:主要的原因问题是去除空白,这我能够向后步进通过网格
如果IsDBNull以便(CellValue)OrElse运算CellValue是Nothing OrElse运算... – Aaron
你需要重新安排你的支票让'CellValue为Nothing '先来...如果它为空,你不能检查它的'ToString()'方法。您还需要向后循环而不是向前循环,否则一旦删除一行,您的索引值将不同步。如果您必须前进,您必须在删除后手动调整'i'的值 – pinkfloydx33