如何检查datagridview单元是否为空 [英] How to check if datagridview cell is Null

查看:127
本文介绍了如何检查datagridview单元是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的datagridview的单元格的值为Null,我想显示一条消息。
请告知如何做。
谢谢,最好的问候,

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,

Furqan

推荐答案

您需要检查 属性 nofollow> DataGridViewCell Nothing (等效于 null in C#)。

You need to check if the Value property of the DataGridViewCell is Nothing (the equivalent of null in 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



如果您想通知用户何时尝试将保留为空的单元格保留为空白,您需要在 CellValidating 事件处理程序方法。例如:


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 CellValidating event handler method. For example:

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

这篇关于如何检查datagridview单元是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆