[英]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.