.net 如何检查datagridview单元格是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4942593/
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
How to check if datagridview cell is Null
提问by Furqan Sehgal
I want to display a message if the value of cell of my datagridview is Null. Please advise how to do it. Thanks and best regards,
如果我的 datagridview 的单元格值为 Null,我想显示一条消息。请指教怎么做。谢谢和最好的问候,
Furqan
富尔坎
回答by Cody Gray
You need to check if the Valuepropertyof the DataGridViewCellis Nothing(the equivalent of nullin C#).
您需要检查Value属性的DataGridViewCell是Nothing(在相当于null在C#)。
You can do that with the following code:
您可以使用以下代码执行此操作:
If myDataGridView.CurrentCell.Value Is Nothing Then
MessageBox.Show("Cell is empty")
Else
MessageBox.Show("Cell contains a value")
End If
If you want to inform the user when they try to leave the cell that it has been left empty, you need to use similar code in the CellValidatingevent handler method. For example:
如果要在用户尝试离开单元格时通知用户它已被留空,则需要在CellValidating事件处理程序方法中使用类似的代码。例如:
Private Sub myDataGridView_CellValidating(ByVal sender As Object,
ByVal e As DataGridViewCellValidatingEventArgs)
Handles myDataGridView.CellValidating
If myDataGridView.Item(e.ColumnIndex, e.RowIndex).Value Is Nothing Then
' Show the user a message
MessageBox.Show("You have left the cell empty")
' Fail validation (prevent them from leaving the cell)
e.Cancel = True
End If
End Sub

