我在使用C# DateTimePicker控件时出现了DBnull的错误
上网查资料使用以下方法解决了问题,但是程序必须在起动后,对DateTimePickerk
控件进行选择操作后,其日期值才能存入数据库;如果不对其进行操作,其结果数据库相应字段的值为NULL,有什么办法可以在不对DateTimePicker控件进行操作的情况下
将DateTimePicker的默认值存入数据库??????
代码如下:我先在DateTimePicker控件中添加一个object类型的属性 DBValue
public class DBDateTimePicker:DateTimePicker
{
 
public DBDateTimePicker()
{
//
// TODO: Add constructor logic here
//
}
 
        public object DBValue
        {
            get
            {
                if (this.Checked)
                    return base.Value;
                else
                    return System.DBNull.Value;
            }
            set
            {
                if (System.Convert.IsDBNull(value))
                    this.Checked=false;
                else
                    this.Value = Convert.ToDateTime(value);
            }
        }
}然后将这个DBValue属性进行绑定
this.EffectiveDate.DataBindings.Add(BValue",this.CADData,"EffectiveDate");