确保数据网格视图中的单元格不会留空。 [英] Ensuring a cell in a datagridview is not left blank.

查看:95
本文介绍了确保数据网格视图中的单元格不会留空。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个表单上有一个DataGridView,用户可以编辑然后更新,以便对链接到的数据库进行更改。出于这个原因,我不希望任何单元格留空。目前我正在使用此代码:

 如果 datagrdSnippets.Item(e.ColumnIndex,e.RowIndex) .Value   没什么 然后 
' 向用户显示消息
MsgBox( 请确保单元格已被赋值

' 失败验证(阻止它们离开单元格)
e.Cancel = True
结束 如果





在DSG的单元格验证属性中,但是当我编辑单元格并将其留空时,没有任何反应。我的代码有什么问题或我需要使用其他方法吗?



谢谢:)

解决方案

< blockquote>在我看来,没有发生的原因是你的代码:

 datagrdSnippets.Item(e.ColumnIndex,e.RowIndex).Value   没什么 





我建议您使用: String.IsNullOrEmpty [ ^ ]方法;)



请看看这里:

解决方法:验证数据Windows窗体DataGridView控件 [ ^ ]

如何:验证Windows窗体DataGridView控件中的数据 [ ^ ]


I have a DataGridView on one of my forms that the user can edit and then update so changes are made to the database it is linked to. For this reason i do not want any of the cells to be left blank. Currently i am using this code:

If datagrdSnippets.Item(e.ColumnIndex, e.RowIndex).Value Is Nothing Then
    ' Show the user a message
    MsgBox("Please ensure the cell has been given a value")

    ' Fail validation (prevent them from leaving the cell)
    e.Cancel = True
End If



In the cell validating property of the DSG, however when i edit a cell and leave it blank nothing happens. Is there something wrong in my code or do i need to use another method?

Thanks :)

解决方案

In my opinion the reason of "nothing happens" is your code:

datagrdSnippets.Item(e.ColumnIndex, e.RowIndex).Value Is Nothing



I would suggest you to use: String.IsNullOrEmpty[^] method ;)

Please, have a look here:
Walkthrough: Validating Data in the Windows Forms DataGridView Control[^]
How to: Validate Data in the Windows Forms DataGridView Control[^]


这篇关于确保数据网格视图中的单元格不会留空。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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