Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, July 27, 2013 2:02 PM
Can anyone show me how to bind a checkbox to a binding source.
This may seem simple but I am completely baffled.
this.checkBox4.DataBindings.Add("Checked", this.bs, "A_F_Cash", true);
using a windows.form vs 2012
Many thanks
All replies (2)
Saturday, July 27, 2013 3:46 PM ✅Answered
The answer might depend on what type the datasource of your DataBinding 'this.bs' is, for instance is it a DataTable (then, 'A_F_Cash' should be a Column of that table) or is it a DataSet (then the compiler will try to bind to a DataTable named 'A_F_Cash' within that DataSet and that, of course, fails).
wizend
Saturday, July 27, 2013 3:52 PM ✅Answered | 1 vote
I'm assuming that you might be getting a DBNull error? What you need to do is handle the Format event to take care of that.
this.checkBox4.DataBindings.Add("Checked", this.bs, "A_F_Cash", true);
this.checkBox4.DataBindings[0].Format += new ConvertEventHandler(CheckBoxFormatHandler);
void CheckBoxFormatHandler(object sender, ConvertEventArgs e)
{
if (e.Value == System.DBNull.Value)
e.Value = false;
}
You can use the same CheckBoxFormatHandler() for all the CheckBoxes on your Form. Alternatively, you could write your own CheckBox class, sub-classed from CheckBox to do this sort of thing directly in your class.
~~Bonnie Berent DeWitt [C# MVP]