Sunday, February 26, 2012

DataBinding to radio button

I have a windows form containing some text boxes and radio buttons bount to an SQL Server database, with code like the following:

this.tbBorrowerLastName.DataBindings.Add (new Binding("Text", dsData.Tables["Results"],"BorrowerLastName"();

this.rbMale.DataBindings.Add(new Binding("Checked", dsData.Tables["Results"],"Male"))

If I add a new row and try to move there using the code below, nothing happens (neither movement to a new position nor error message), unless I remove the radio button bindings. (The same thing occurs if I attempt to move to any record that has a null value in the database field for a radio button).

myLastRow = dsData.Tables["Results"].NewRow();

dsData.Tables["Results"].Rows.Add(myLastRow);

I attempt to move by changing the value of the position property of the BindingContext object.

How can I retain the radio button bindings and still be able to add and move to a new row?

Any help would be appreciated.

I was able to figure my own solution.

The problem was a null value for the checked property of the radio buttons. I tried adding a default value to Visual Studio and to the database, but neither of those solved the problem.

Inbetween the two lines of code in my earlier post (beginning myLastRow and dsData.Tables respectively), I added:

myLastRow["Male"]=true;

myLastRow["Female"]=false;

Problem solved.