How to bind a CheckBox to a bool typed DbColumn that is nullable?

后端 未结 2 1981
挽巷
挽巷 2021-01-19 23:40

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 whi

2条回答
  •  無奈伤痛
    2021-01-20 00:31

    The easist way I know, is derive from CheckBox class, add "DataValue" property which can handle DBNull values and bind the data to "DataValue" property:

    public class DataCheckBox : CheckBox {
        public virtual object DataValue {
            get { return this.Checked; }
            set {
                if ( value == null || value is DBNull ) {
                    this.CheckState = CheckState.Indeterminate;
                }
                else {
                    this.Checked = (bool)value;
                }
            }
        }
    }
    

提交回复
热议问题