Binding Navigator problem with bound Check Box


When I click on the "AddNewItem" button on the BindingNavigator the record count number increases but it does not display "new" blank controls.

The problem seems to be with checkboxes that are bound to a datasource on the form.

cbActive.DataBindings.Add("Checked", BindingSource1, "Active")

If I comment this out then the "AddNewItem" works as expected.

I have tried swapping the code for:
cbActive.DataBindings.Add("CheckState", BindingSource1, "Active", True, DataSourceUpdateMode.OnValidation, CheckState.Unchecked)

But this causes invalid NULL issues when I save the data.

Please help.

Thanks, Greg
Who is Participating?
spen_langConnect With a Mentor Author Commented:
OK, I used the following code in the end:    

Private Sub btnAddNew_Click(sender As System.Object, e As System.EventArgs) Handles btnAddNew.Click

        Dim drvUsers As DataRowView

        drvUsers = Me.BindingSource1.AddNew()
        cbActive.Checked = True
        cbEmployeeAccidentRate.Checked = False
        cbInvoiceAccuracy.Checked = False
        cbCreateEditUsers.Checked = False
        cbSettings.Checked = False
        txtWindowsUsername.Text = "(NEW)"
        txtName.Text = ""

        drvUsers.Row.Item("UserID") = txtWindowsUsername.Text
        drvUsers.Row.Item("Username") = txtName.Text
        drvUsers.Row.Item("InvoiceAccuracy") = cbInvoiceAccuracy.Checked
        drvUsers.Row.Item("EmployeeAccidentRate") = cbEmployeeAccidentRate.Checked
        drvUsers.Row.Item("Settings") = cbSettings.Checked
        drvUsers.Row.Item("Users") = cbCreateEditUsers.Checked
        drvUsers.Row.Item("Active") = cbActive.Checked


    End Sub

Open in new window

Jacques Bourgeois (James Burger)PresidentCommented:
To add a new item, the navigator has to call the standard constructor of the class that provides the data for the controls. If the class does not have a standard constructor, a New() with not parameters, then the navigator is not able to add a new item, because it does not know how to pass parameters to the constructor that is there.

I would expect an exception in such a case, but I haven't used the BindingNavigator since 2003 or so, and I cannot remember how it behave in such a case. I just remember encountering the same problem as yours and solving it by adding a default constructor to my class.

I have stopped using controls or tools that rely too much or only on binding. They hide a lot of things underneath, and the time you can save by using them is usually lost in debugging and maintenance because you do not see the code, and many little details of their use are often hidden in places you would not think of looking for in the documentation.

Would you have thought of looking for BindingSource.AddNew Method in the documentation to understand what is happening to you? The BindingSource is used by the BindingNavigator under the hood when you click on the button.

And in that page of documentation, would you have understood the meaning of "If the underlying list is not an IBindingList, the item is automatically created through its public default constructor."?

And by doing my little search to give you that information, I at least found out that if there is no default constructor, you should get an InvalidOperationException instead of getting nothing. But the exception might be eaten by the BindingNavigator, so you never receive it. I have seen that happen. Who knows, I cannot find anything about that in the BindingNavigator documentation, because all that happens when you click on the AddNewItem button is hidden behind the property of the same name, with not specifics about what happen in the navigator when it does not find the constructor.

That is typical of working with controls that are supposed to simplify your work through binding.

Writing your own code to do the same thing can take a while, but at least, you have an idea of what is happening when something goes wrong. And after you've done it once, you have a template that you can easily reuse in similar situations.
spen_langAuthor Commented:
Ok, the problem I now have is that when the checkbox is unchecked the dataset value is NULL instead of 0 (false).

To bind the object i am using
cbActive.DataBindings.Add("CheckState", BindingSource1, "Active", True, DataSourceUpdateMode.OnValidation, CheckState.Unchecked)
spen_langAuthor Commented:
I have now set the code as:
cbActive.DataBindings.Add("CheckState", BindingSource1, "Active", True, DataSourceUpdateMode.OnValidation, False)

But I am still getting NULL values when the checkbox is unchecked.

Please help
spen_langAuthor Commented:
Not enough feedback
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.