datagridview datatable不为null [英] datagridview datatable not null

查看:167
本文介绍了datagridview datatable不为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

datagridview dgv从数据表dt填充,而数据表dt从数据库中的表填充.
数据库表具有以下字段:

CustID int不为null
fieldNo int不为null
field3 varchar(10)null,
...
...

在dgv上,我可以添加一条新记录,因为它允许添加新行.

问题:
字段FieldNo在dgv上隐藏.添加新行时,显然用户没有添加fieldNo.结果,dgv给出了该字段不允许为null的错误.
请问如何解决这个问题?
请注意,它必须具有一个空值,因为一旦用户完成添加新行,则数据库存储的proc将负责在插入期间为表选择正确的fieldNo.
有什么想法吗?

谢谢

datagridview dgv is populated from datatable dt which is populated from a table in the database.
The database table has fields such as:

CustID int not null
fieldNo int not null
field3 varchar(10) null,
...
...

On the dgv, I can add a new record as it allows adding a new row.

Question:
The field FieldNo is hidden on the dgv. When adding a new row, obviously the user is not adding a fieldNo. As a result, the dgv gives an error that the field does not allow null.
What is the way to solve this please?
Note that it has to have an empty value because once the user has finished adding a new row, then the database stored proc will take care of choosing the correct fieldNo for the table during the insert.

Any thoughts please?

Thanks

推荐答案

您为什么绑定网格以包括该字段?别.如果用户由于任何原因看不到ID字段,请不要将其包括在绑定中.用代码设置DGV来创建您的列,而不是让DGV自动创建它们.只是不要绑定到ID列.

更好的解决方案是不要让用户直接在DGV中添加数据.我总是提供处理此问题的创建新记录"表格.在将数据添加到数据库之前,控制数据输入过程和验证数据要容易得多.
Why did you bind the grid to include that field? Don''t. If the user can''t see the ID field for any reason, don''t include it in the bindings. Setup your DGV in code to create your columns instead of letting the DGV automatically create them. Just don''t bind to the ID column.

The better solution is to not let users add data directly in a DGV. I always supply a "create new record" form that handles this. It''s much easier to control the data entry process and validate the data before adding it to the database.


然后为什么不将列filedNo设置为允许空值).
then why don''t you set the column filedNo to allow null(s).


这篇关于datagridview datatable不为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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