I am having problems with binding to a check box. It works fine until I
attempt to add a new record to the dataset through the binding context
object.
The code that adds the new record to the dataset is listed below
Me.BindingContext(dsDataSet, TableName).EndCurrentEdit()
Me.BindingContext(dsDataSet, TableName).AddNew()
The add new statement generates an error and a new record is not shown. Is there any solution

Check box data binding, AddNew doesn't work
Richard Petkiewicz
michael olson
Try this:
Dim sql As String = "Select CountryName,IsFirstCountry from tblCountry where Country= -1"
Dim sa As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, conn)
Dim ds As New DataSet
Try
sa.Fill(ds, "Country") ' returns an empty dataset but with the correct structure
Dim dr As DataRow = ds.Tables(0).NewRow ' creates a new blank row
' *******
dr.Item(1) = 0 ' 1 is the column with the check box
' *******
ds.Tables(0).Rows.Add(dr) ' add the blank row to the dataset
Finally
sa.Dispose()
End Try
The binding part will be :
Me.txtCountry.DataBindings.Add(New Binding("Text",ds,"Country.CountryName"))
Me.CheckBox1.DataBindings.Add(New Binding("Checked", ds, "Country.IsFirstCountry"))
The trick is to first fill the "IsFirstCountry" field before binding the "CheckBox1" to the "IsFirstCountry" table field.
I hope this will help!
welikeike
So in your 7 record example (6 existing +1 new), the count property is 7 - but these are records 0 thru 6. Try Count-1 instead of just Count:
chkApproval.DataBindings.Clear()
Me.BindingContext(Ds, "eBackToProvider").EndCurrentEdit()
Me.BindingContext(Ds, "eBackToProvider").AddNew()
Ds.Tables("eBackToProvider").Rows(Me.BindingContext(Ds, "eBackToProvider").Count-1).Item("Approval") = 0
Ds.Tables("eBackToProvider").AcceptChanges()
chkApproval.DataBindings.Add("Checked", Ds, "eBackToProvider.Approval")
Does that fix it
Ryan Jones
Hi
I got the ssame problem then after searching this site i found an easy solution..
I recommend you to visit The knowledge base articles 326440 and 321504 titled as follows:
BUG: AddNew Method of CurrencyManager Fails with Bound CheckBox
BUG: Check Box Is Not Cleared When You Call AddNew() Method on a DataSet
The problem and its solution is found there
God luck
geman
As you advised I added the following after AddNew record:
Ds.Tables("eBackToProvider").Rows(Me.BindingContext(Ds, "eBackToProvider").Count).Item("Approval") = 0
Ds.Tables("eBackToProvider").AcceptChanges()
So, codes like the following:
chkApproval.DataBindings.Clear()
Me.BindingContext(Ds, "eBackToProvider").EndCurrentEdit()
Me.BindingContext(Ds, "eBackToProvider").AddNew()
Ds.Tables("eBackToProvider").Rows(Me.BindingContext(Ds, "eBackToProvider").Count).Item("Approval") = 0
Ds.Tables("eBackToProvider").AcceptChanges()
chkApproval.DataBindings.Add("Checked", Ds, "eBackToProvider.Approval")
But counting rows - Me.BindingContext(Ds, "eBackToProvider").Count - caused an error. Suppose I currently have 6 records. Error message is "There is no row at position 7."
After AddNew VB counts 7 rows but when assigning values it does not recognize 7th row.
gregger
Jonas Rosqvist
I implemented these same two lines of code successfully in a Button.Click event in a test project. I had a datagrid bound to my DataSet, if you can describe the binding you use for your checkbox, I can test that as well.
KyleB
You might try something like the following:
Me.BindingContext(Ds, "eBackToProvider").AddNew()
Me.BindingContext(Ds, "eBackToProvider").Current.Item("Approval") = 0
It would probably be better to create an actual BindingManagerBase objec to work with throughout the routine. Start your code with:
Dim BMB as BindingManagerBase = Me.BindingContext(Ds, "eBackToProvider")
and then:
BMB.AddNew()
BMB.Current.Item("Approval")=0
I'm really not sure beyond that... It has to be an issue with the way you are data bound. The checkbox should not*space
space*the AddNew method. Try to edit your code so that all changes are done through the BindingManagerBase rather than through the DataSet.
Peter Mo.
I'm perplexed that this is a simple data entry form with check box binding. AddNew doesn't work with data binding check box Microsoft was not aware of this This is ridiculous. Have you ever seen data form with data bound check boxex
Anyway, do you have any other idea I'm desperate my project dead line is over. I have to solve this ASAP. Thank you for your any help.
Darin V
First, suspend binding on your controls. Then, add your new record and assign some default values to each field. Call AcceptChanges method of the dataset and then resume binding.
I may need to play with it a bit more... this seems easy enough... we are probably missing something simple...
SpaceGuy
chkApproval.DataBindings.Add("Checked", Ds, "eBackToProvider.Approval")
When I click "Add" button
Me.BindingContext(Ds, "eBackToProvider").EndCurrentEdit()
Me.BindingContext(Ds, "eBackToProvider").AddNew()
ds_PositionChanged()
I have:
Private Sub ds_PositionChanged()
lblNavLocation.Text = (((Me.BindingContext(Ds, "eBackToProvider").Position + 1).ToString + " of ") _
+ Me.BindingContext(Ds, "eBackToProvider").Count.ToString)
End Sub
lblNavLocation.Text shows "1 of 'rows count + 1' but the form still shows old record not added new record. No error message.
I found when there are check boxes bound to data AddNew doesn't work properly.
So, I clear binding before adding a record as below
chkApproval.DataBindings.Clear()
chkApproval.Checked = False
Me.BindingContext(Ds, "eBackToProvider").EndCurrentEdit()
Me.BindingContext(Ds, "eBackToProvider").AddNew()
ds_PositionChanged()
It works fine showing new record on the form. But I have to re-bind after adding a record. So,
chkApproval.DataBindings.Clear()
chkApproval.Checked = False
Me.BindingContext(Ds, "eBackToProvider").EndCurrentEdit()
Me.BindingContext(Ds, "eBackToProvider").AddNew()
ds_PositionChanged()
chkApproval.DataBindings.Add("Checked", Ds, "eBackToProvider.Approval")
Now I have an error message:
"An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll
Additional information: Object cannot be cast from DBNull to other types."
Can you help me