【问题标题】:how to check if the cell value is nothing or not in vb.net?如何在 vb.net 中检查单元格值是否为空?
【发布时间】:2010-11-30 22:44:40
【问题描述】:


我在 vb.net 中使用网格视图。
这是代码...

If Not DataGridView1.SelectedRows.Count = 0 Then
                i = DataGridView1.SelectedRows(0).Index
                If DataGridView1.Rows(i).Cells(0).Value <> Nothing Then
                    namebox.Text = Trim(DataGridView1.Rows(i).Cells(0).Value)
                    salarybox.Text = DataGridView1.Rows(i).Cells(1).Value
                End If
End If

现在如果单元格中没有任何内容,那么它将显示异常......
像这样……

Operator '<>' is not defined for type 'DBNull' and 'Nothing'.

这是在更改选定单元格时将调用代码。
我试图获取所选单元格的值并将其放入一个文本框中。

【问题讨论】:

    标签: vb.net gridview


    【解决方案1】:

    您不想使用&lt;&gt; 运算符,您应该使用value IsNot Nothing 来检查它是否是IsNot Nothing,或者反过来使用Is Nothing 来检查值是否为Is Nothing

    还有一个原因是DBNullNothing 类型没有比较器,所以如果是这种情况,您需要检查两者。像

    If value IsNot Nothing AndAlso value <> DBNull.Value Then
    
       ''#Do something
    
    End If
    

    【讨论】:

      【解决方案2】:

      改变

      If DataGridView1.Rows(i).Cells(0).Value <> Nothing Then
      

      If Not DataGridView1.Rows(i).Cells(0).Value Is DBNull.Value Then
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多