Как привязать CheckBox к типу DbColumn типа bool, который можно обнулять?
В Windows Forms (.NET 2.0, Visual Studio 2005 SP1): я набралDataSet
с колонкой, тип которойSystem.Boolean
, который имеет значение NULL и какое значение по умолчаниюDBNull
, у меня естьForm
, содержащийCheckBox
контролировать, что я хочу привязать к предыдущему значению столбца.
Checked
property to the column via the designer : it works great, only if the default value for the column is set to either True
or False
.
I have tried to bind the CheckState
property to the column via the designer, and attaching my own Format
and Parse
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 the CheckBox
bindings : the event handlers are still never get called...
Binding b=new Binding("CheckState", _BindingSource, "MyColumn", false, DataSourceUpdateMode.OnPropertyChanged, DBNull.Value);
Как примечание:DBNull
значение допустимо только при поступлении изDataSet
(это означает, что значение никогда не было установлено). Но пользователь должен иметь возможность только установить значениеTrue
или жеFalse
черезCheckBox
.
Для справки, вот код методов разбора и форматирования:
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;
}