当用户从未输入单元格时,如何验证 DataGridView 单元格不为空?
我只想检查并确保单元格中有一些数据。我的情况是 DataGridView 有 3 列。当有人添加新记录时,行为空白。第一列是组合框,接下来的两列只是纯文本。如果有人只是在其中一个文本框中输入内容并且没有从组合框中进行选择,那么当他们按下“保存”时,会抛出异常,因为数据库需要该组合框值。我如何检查该单元格并确保有一个值? CellValidating 将不起作用,因为单元格永远不会获得焦点并失去焦点。
我最终检查了 RowValidating 事件中的列并循环遍历这些列,检查它们是否为空。这感觉很脏,这是唯一的方法吗?
I just want to check and make sure that a cell has some data in it. My situation is I have 3 columns in a DataGridView. When someone adds a new record the rows are blank. The first column is a ComboBox, the next two are just plain text. If someone just enters in one of the text boxes and doesn't select from the combobox, when they press save an exception is thrown since the DB wants that combobox value. How can I check that cell and make sure there is a value? CellValidating won't work since the cell never gets focus and loses focus.
I did end up checking the column in the RowValidating event and looped through the columns, checking if they are empty. This feels dirty, is that the only way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
验证控制?他们可以为您提供客户端和服务器端验证。
http://msdn.microsoft.com/en-us/library/aa479013。 ASPX
Validating controls? They can give you both client side and server side validation.
http://msdn.microsoft.com/en-us/library/aa479013.aspx