In Windows Forms (.NET 2.0, Visual Studio 2005 SP1) : I have a typed DataSet
, with a column which type is System.Boolean
, which is nullable and which default value is DBNull
. I have a Form
, containing a CheckBox
control that I want to bind to the prior column value.
- I have tried to bind the
Checked
property to the column via the designer : it works great, only if the default value for the column is set to eitherTrue
orFalse
. I have tried to bind the
CheckState
property to the column via the designer, and attaching my ownFormat
andParse
event handlers but they never get called :b.Format+=delegate(object sender, ConvertEventArgs cevent) { cevent.Value=DoFormat((CheckState)cevent.Value); // cf. end of the question }; b.Parse+=delegate(object sender, ConvertEventArgs cevent) { cevent.Value=DoParse(cevent.Value); // cf. end of the question };
I have tried to create a custom
Binding
instance in the code, attach my event handlers and add it to theCheckBox
bindings : the event handlers are still never get called...Binding b=new Binding("CheckState", _BindingSource, "MyColumn", false, DataSourceUpdateMode.OnPropertyChanged, DBNull.Value);
As a note : a DBNull
value is acceptable only when coming from the DataSet
(it means the value has never been set). But the user should only be able to set the value to True
or False
via the CheckBox
.
For reference, here is the code of the parsing and formatting methods :
internal static CheckState DoParse(object value)
{
if ((value==null) || (value is DBNull))
return CheckState.Indeterminate;
bool v=Convert.ToBoolean(value);
return (v ? CheckState.Checked : CheckState.Unchecked);
}
internal static object DoFormat(CheckState value)
{
switch (value)
{
case CheckState.Checked:
return true;
case CheckState.Indeterminate:
return DBNull.Value;
case CheckState.Unchecked:
return false;
}
return null;
}