[英]How to check if datagridview cell is Null
如果我的datagridview的单元格的值为Null,我想显示一条消息。 请告知如何做。 谢谢和最诚挚的问候,
Furqan
您需要检查DataGridViewCell
的Value
属性是否为Nothing
(在C#中为null
)。
您可以使用以下代码进行操作:
If myDataGridView.CurrentCell.Value Is Nothing Then
MessageBox.Show("Cell is empty")
Else
MessageBox.Show("Cell contains a value")
End If
如果要在用户尝试将其保留为空的状态时通知用户,则需要在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.