简体   繁体   English

WinForm数据绑定复选框的值

[英]WinForm Databound Checkbox Value

I get an exception when my databound checkbox is confronted with a null value: 当我的数据绑定复选框遇到null值时,我得到一个异常:

{"Cannot bind to the property or column boolValue on the  DataSource.\r\nParameter name: dataMember"}

Is there a way to have the checkbox default to unchecked on a null value. 有没有一种方法可以使复选框默认为不选中空值。 I have looked in the databinding properties and on MSDN but can not find anything that seems relevant 我已经查看了数据绑定属性和MSDN上的内容,但是找不到任何相关的内容

You can use Binding.NullValue property for that. 您可以Binding.NullValue使用Binding.NullValue属性。 But make sure you also set Binding.FormattingEnabled property to true . 但是请确保还将Binding.FormattingEnabled属性设置为true

For instance, something like this: 例如,如下所示:

var dataSource = ...;
checkBox.DataBindings.Add("Checked", dataSource, "{PropertyName}",
    true, // formattingEnabled
    DataSourceUpdateMode.OnPropertyChanged,
    false // nullValue
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM