检查DataGridView

时间:2016-11-21 06:53:52

标签: vb.net datagridview rows

每次代码加载时我都在编写代码,检查DatagridView中第三列的值;如果它为空,则显示"清空",但当它内有值时,它显示"非空"

我正在使用此代码

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Try
        For each row as DataGridViewRow in DataGridView1.Rows
                If row.Cells(2).Value is Nothing Then
                   MsgBox("Empty")
                Else
                   MsgBox("Not Empty")
                EndIf
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

但是我得到的数据不正确,我有DataGridView的值

--------------------------
    |NAME | AGE | TIME IN |
--------------------------
     PAUL    18         

但它显示

"Not Empty"

在第一条消息和

"Empty"

在第二条消息上,但我只想显示一条消息,说“"清空”#34;因为第一行的第三列不包含任何值。

1 个答案:

答案 0 :(得分:0)

这解决了我的问题:)谢谢

Try
        For each row as DataGridViewRow in DataGridView1.Rows
                If row.Cells(2).Value is Nothing Then
                   MsgBox("Empty")
                    Exit For
                Else
                   MsgBox("Not Empty")
                   Exit For
                EndIf
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try