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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spen_langAuthor Commented:
Not enough feedback
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.