保存更改时,如何避免 DataGridView 中的 DataTable 中出现 System.DBNull?
我有一些 DataGridView,每个 DataGridView 显示具有多个 DataTable 的 DataSet 中的一个 DataTable。
将新行添加到 DataGridView 并将其保存到 DataSet 时,布尔列具有 System.DBNull-Value,这会导致稍后出现异常(在其他应用程序中读取 DataSet 时)。
仅当我的布尔列中的复选框被选中然后取消选中时,在保存数据集时它的有效值为 false。
将复选框列添加到 DataGridView 或保存它们时,如何确保复选框列的默认值始终为 false?
I have some DataGridViews each displaying one DataTable from a DataSet with multiple DataTables.
When adding a new row to a DataGridView and saving it to the DataSet, bool columns have a System.DBNull-Value which causes an exception later (when reading the DataSet in an other application).
Only if the checkboxes in my bool column are checked and then unchecked, it has a valid value of false when saving the DataSet.
How can I ensure, that checkbox columns always have a default value of false when adding them to my DataGridView or saving them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用存储过程吗?如果是这样,您可以在其中设置默认值或为数据库中的列设置默认值。
另一种方法是分配 DataTable.Column 的 DefaultValue 属性。
您可以使用 DataColumn 对象的 DefaultValue 属性。
您还可以在每次添加行时指定值:
Are you using stored procedures? If so, you can set the default value in there or set a default value for the column in the database.
Another route would be to assign the DefaultValue property of the DataTable.Column
You use the DefaultValue property of the DataColumn object.
You can also specify the values each time you add a row:
在数据集设计器中,您可以选择一个数据表列并将AllowDBNull 设置为false,将DefaultValue 设置为False。这样该值将为 false 而不是 DBNull。
In the DataSet designer you can select a DataTable column and set AllowDBNull to false and DefaultValue to False. This way the value will be false rather than DBNull.