0
votes

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?

2

2 Answers

2
votes

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
0
votes

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.