方法一:
https://www.icode9.com/content-1-251835.html
如果它是一个位(0或1),您必须为Binding添加Format事件处理程序:
Binding bind = new Binding("Checked", bindingSource5, "SchoolContacted");
bind.Format += (s,e) => {
e.Value = (int)e.Value == 1;
};
cbSchoolFri.DataBindings.Add(bind);
使用Binding时,这是一项非常基本的任务.
方法二:
Binding binding = new Binding("CheckState", MainBS, "CHK", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
binding.FormattingEnabled = true;
binding.NullValue = CheckState.Indeterminate;
checkBox5.DataBindings.Add(binding);
方法三:
在我们的项目中数据库中所有的bool值都是用Char(1)来记录的,‘T‘为真 'F’则为假。有意思的是DataGridView上的CheckBoxColumn有TrueValue和FalseValue属性,如果你想用三态还有一个Indeterminate属性。这样只需要将TrueValue设置为’T’将FalseValue设置为‘F’就可以轻松绑定。但是CheckBox控件就没有这些属性,只能通过能隐式转换为bool值的属性绑定(bool类型,字符串“True”和”False”),否则抛出不是有效的布尔值的异常。那么如何将’T’转化为true将’F’转化为false来绑定到CheckBox上呢?
方法一、在Business类上加bool型的属性,或建VO类。将原有属性封装为bool型。这种方法可行。但是需要一定的工作量,而且在类中加入一些重复的属性(只是类型不同)感觉不爽。
方法二、使用Binding的Format和Parse事件。这个是我推荐的方法,其实微软已经想到了绑定属性与被绑定属性可能要存在类型转换的问题,这两个事件就是为这种情况而预留的接口。
关于这两个事件的官方文档可以参考:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.binding.parse.aspx
Format是控件绑定时和数据源的值发生变化时调用,Parse是离开焦点时调用。其实Parse掉用时就是改变数据源时,因此Parse调用完就会调用Format.
Format顾名思义就是转化为可以显示的值,那么CheckBox就是用bool值显示。在我这个情况中就是由string转化为bool。Parse就是解析为数据源的所需要的值,在我这个情况就是由bool转化为string。但是考虑到三态的可能因此我并不是进行string与bool的转化。而是string与CheckState的转化。
01.
/// <summary>
02.
/// Binds the CheckBox
03.
/// </summary>
04.
/// <param name="source">Binding Source</param> www.it165.net
05.
/// <param name="checkBox">The Combo Box</param>
06.
/// <param name="bindingField">The binding field name</param>
07.
protected
void
BindingCheckBox(BindingSource source, CheckBox checkBox,
string
bindingField)
08.
{
09.
Binding binding = checkBox.DataBindings[
"CheckState"
];
10.
if
(binding !=
null
)
11.
{
12.
checkBox.DataBindings.Remove(binding);
13.
}
14.
Binding checkBoxBinding =
new
Binding(
"CheckState"
, source, bindingField,
true
, DataSourceUpdateMode.OnPropertyChanged);
15.
checkBoxBinding.Format += BindingCheckBox_Format;
16.
checkBoxBinding.Parse += BindingCheckBox_Parse;
17.
checkBox.DataBindings.Add(checkBoxBinding);
18.
}
19.
/// <summary>
20.
/// Format event for checkBox binding
21.
/// </summary>
22.
/// <param name="sender">The source of the event. Here is Binding.</param>
23.
/// <param name="e">Provides data for the System.Windows.Forms.Binding.Format and System.Windows.Forms.Binding.Parse events.</param>
24.
void
BindingCheckBox_Format(
object
sender, ConvertEventArgs e)
25.
{
26.
if
(e.DesiredType ==
typeof
(CheckState))
27.
{
28.
if
(e.Value !=
null
&& e.Value.ToString() ==
"T"
)
29.
{
30.
e.Value = CheckState.Checked;
31.
}
32.
else
if
(e.Value !=
null
&& e.Value.ToString() ==
"M"
)
33.
{
34.
e.Value = CheckState.Indeterminate;
35.
}
36.
else
37.
{
38.
e.Value = CheckState.Unchecked;
39.
}
40.
}
41.
}
42.
/// <summary>
43.
/// Parse event for checkBox binding
44.
/// </summary>
45.
/// <param name="sender">The source of the event. Here is Binding.</param>
46.
/// <param name="e">Provides data for the System.Windows.Forms.Binding.Format and System.Windows.Forms.Binding.Parse events.</param>
47.
void
BindingCheckBox_Parse(
object
sender, ConvertEventArgs e)
48.
{
49.
if
(e.DesiredType ==
typeof
(
string
))
50.
{
51.
switch
((CheckState)e.Value)
52.
{
53.
case
CheckState.Checked:
54.
e.Value =
"T"
;
55.
break
;
56.
case
CheckState.Indeterminate:
57.
e.Value =
"M"
;
58.
break
;
59.
default
:
60.
e.Value =
"F"
;
61.
break
;
62.
}
63.
}
64.
}
这样我们就可以使用这个方法来将CheckBox与绑定源进行绑定了。当然你的绑定源也可以是一般的对象。在这里"M"来做不确定值,这是项目的约定。这里我省去了判断CheckBox是否是允许三态,而是判断绑定的值是否是不确定态,如果是不确定态则认为CheckBox就是允许三态,否则非真即假。