繁体   English   中英

datagridview单元格vb.net的空值

[英]Null Value for datagridview cell vb.net

如何在datagridview中获取空值我在单元格验证事件中做到了这一点,但它似乎起作用了。 我希望用户添加一个新行,并且有人强迫他提供一个ID或删除该行。 我究竟做错了什么。 这不是一个怎么做的问题。 这是一个错误的问题。 因此,现在它检测到为空,但是一旦我纠正了单元格,它仍然有一定的空间让我离开该行。

          If DataGrid.CurrentCell.ColumnIndex = 0 Then
        If IsDBNull(DataGrid.CurrentCell.Value) Then
            Msgbox("Cannot be Null")
              e.cancel = true
        ElseIf Not IsDBNull(DataGrid.CurrentCell.Value) Then
              e.cancel = False

        End If
    End If

所以我尝试了这个,对我有用。 它只是使用e.formatedvalue。 当前单元格值是编辑前后的单元格值,而formatedvalue是键入它的内容。 我想我现在明白了,所以这里是编码

              If grdDataGrid.CurrentCell.ColumnIndex = 2 Then
        If e.FormattedValue = "" Or IsDBNull(e.FormattedValue) Then
            MsgBox("Cannot be Null")
            e.Cancel = True
            Exit Sub

        End If
    End If

它们也是不同的方式,例如将列属性调整为不允许为null,但是由于列属性是从数据库继承的,因此我决定使用此属性。

我认为这应该是可行的...

If DataGrid.CurrentCell.ColumnIndex = 0 Then
    If IsDBNull(DataGrid.CurrentCell.Value) Then
          Msgbox("Cannot be Null")
          e.cancel = true
          exit sub
    End If
End If

解决此问题的另一种方法是将新的datagrid单元的默认null值显式设置为要检查的值。 例如,如果要提取字符串值,请将null =设置为空字符串。 您可以使用属性面板进行设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM