datagridview 单元格 vb.net 的空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17204725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:58:50  来源:igfitidea点击:

Null Value for datagridview cell vb.net

vb.netvisual-studio-2012datagridview

提问by shaik ibrahim

How to get for null value in datagridview i did this in the cell validation event but it dosent seem to work. I want the user to add a new row and someone force him to give an ID or delete the row. What am i doing wrong. This is not a how to do question. This is a what is wrong Question. So right now it detects null but once i corrected the cell it still dosent allow me to get out of the row.

如何在 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

回答by shaik ibrahim

So i tried this and it works for me . it simply uses e.formatedvalue. Current Cell Value is the cell value before and after edit while formatedvalue is what is being typed it . i guess i understand now so here is the coding

所以我尝试了这个,它对我有用。它只是使用 e.formatedvalue。Current Cell Value 是编辑前后的单元格值,而 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

They are also different ways like adjusting your column properties to not allowed null but since the column properties inherit from the database i decided to use this.

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

回答by Richard Binning

Another way around this would be to explicitly set the default null value for the new datagrid cell to the value you are checking for. For instance,set the null= to an empty string if you are pulling string values. You can use the properties palette to set this.

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

回答by matzone

I think this should be works ...

我认为这应该是有效的......

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