在Form_load或者窗体方法Form1()中,初始化:
this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.CustomFormat = " ";
在时间控件事件中写入:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
this.dateTimePicker1.CustomFormat = null;
}
在读取值的时候,做判断:
string createTime= “”;
//判断控件值长度是否小于等于1
if (this.dateTimePicker1.Text.Length <=1)
{
createTime= null;
}else{
createTime= dateTimePicker1.Value.ToString(“yyyy-MM-dd”);
}
这种方法稍微有点取巧,也可以自定义控件。