复选框绑定默认值为null
本文关键字:null 默认值 绑定 复选框 | 更新日期: 2023-09-27 18:09:39
我正在将一个复选框绑定到一个bindingsource,如果我不单击复选框,它将返回一个空值到我的数据库
数据绑定代码:
checkGehuwd.DataBindings.Add("Checked", PersonenBindingSource, "gehuwd", true,
DataSourceUpdateMode.OnPropertyChanged);
如何返回默认值false?
问候安迪
您可以这样使用数据绑定到CheckState
属性:
checkBox1.DataBindings.Add("CheckState", bs, "DataFieldName", true,
DataSourceUpdateMode.OnPropertyChanged, CheckState.Indeterminate);
这样,当数据源中字段的值为null时,UI中将显示CheckState.Indeterminate
。
注意
如果您希望列的默认值为
0
或不检查,则将DataColumn
的DefaultValue
设置为0
如果您想让用户也将字段的值设置为null,则只需将
CheckBox
的ThreeState
属性设置为true即可。
dt = new DataTable();
var c1 = dt.Columns.Add("C1", typeof(int));
c1.AllowDBNull = true;
//Uncomment the next statement if you want default value be 0 = unchecked
//c1.DefaultValue = 0;
//Uncomment the next statement if you want to allow the user to set value to null
//checkBox1.ThreeState = true;
var bs = new BindingSource();
bs.DataSource = dt;
checkBox1.DataBindings.Add("CheckState", bs, "C1", true,
DataSourceUpdateMode.OnPropertyChanged, CheckState.Indeterminate);
this.bindingNavigator1.BindingSource = bs;