保存更改时,如何避免 DataGridView 中的 DataTable 中出现 System.DBNull?

发布于 2024-10-05 20:22:29 字数 300 浏览 0 评论 0原文

我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似梦非梦 2024-10-12 20:22:29

您使用存储过程吗?如果是这样,您可以在其中设置默认值或为数据库中的列设置默认值。

另一种方法是分配 DataTable.Column 的 DefaultValue 属性。

您可以使用 DataColumn 对象的 DefaultValue 属性。

' Set default values. '
With myDataSet.Tables("Orders")
    .Columns("Order_Date").DefaultValue = Today
    .Columns("Quantity").DefaultValue = 1
    . . .
End With

' Add the new row. '
Dim aNewRow As DataRow = myDataSet.Tables("Orders").NewRow
myDataSet.Tables("Orders").Rows.Add(aNewRow)

您还可以在每次添加行时指定值:

' Create a new row, set its values and add it. '
Dim aNewRow As DataRow = myDataSet.Tables("Orders").NewRow

With myDataSet.Tables("Orders")
    .Columns("Order_Date") = Today
    .Columns("Quantity") = 1
    . . .
    .Rows.Add(aNewRow)
End With

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.

' Set default values. '
With myDataSet.Tables("Orders")
    .Columns("Order_Date").DefaultValue = Today
    .Columns("Quantity").DefaultValue = 1
    . . .
End With

' Add the new row. '
Dim aNewRow As DataRow = myDataSet.Tables("Orders").NewRow
myDataSet.Tables("Orders").Rows.Add(aNewRow)

You can also specify the values each time you add a row:

' Create a new row, set its values and add it. '
Dim aNewRow As DataRow = myDataSet.Tables("Orders").NewRow

With myDataSet.Tables("Orders")
    .Columns("Order_Date") = Today
    .Columns("Quantity") = 1
    . . .
    .Rows.Add(aNewRow)
End With
意中人 2024-10-12 20:22:29

在数据集设计器中,您可以选择一个数据表列并将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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文