Preface: In. in net, it may be very easy to let datetimepicker display an empty time value, but in fact it is not very easy. At least, it has not been modified and re-painted, you cannot. When you modify this control, you often find bugs. Next, we will introduce a method. At least the source author has studied and has not found many problems.
Source URL
Http://www.codeproject.com/KB/selection/Nullable_DateTimePicker.aspx
You can findSource codeAnd English.
You just need to press the delete key to set the datetimepicker value to null. The actual value is datetime. minvalue. This simple effect may be a lotProgramMembers want to see. But let's take a look at this control.
source Code
the two most important overload methods are.
protected override void oncloseup (eventargs)
protected override void onkeydown (keyeventargs e)
datetimepicker itself supports custom formats, datetimepickerformat. custom is declared to use the custom format.
Public new datetime value
{< br> Get
{< br> If (bisnull)
return datetime. minvalue;
else
return base. value;
}< br> set
{< br> If (value = datetime. minvalue)
{< br> If (bisnull = false)
{< br> oldformat = This. format;
oldcustomformat = This. customformat;
bisnull = true;
}
This. format = datetimepickerformat. custom;
This. customformat = "";
}
Else
{
If (bisnull)
{
This. format = oldformat;
This. customformat = oldcustomformat;
Bisnull = false;
}
Base. value = value;
}
}
}
Source: chakman
C # date control datetimepicker Method for saving null values
Method 1 (recommended ):
Set the attribute showcheckbox of datetimepicker to true.
During window initialization, add the code this. datetimepicker1.checked = false;
When saving the date value to the database, you can save the null value according to If (this. datetimepicker1.checked = false.
Method 2:
Add the following to the window initialization function:
This. datetimepicker1.format = datetimepickerformat. custom;
This. datetimepicker1.customformat = "";
Write in the date change event:
Private void datetimepicker1_valuechanged (Object sender, system. eventargs E)
{
This. datetimepicker1.format = datetimepickerformat. long;
This. datetimepicker1.customformat = NULL;
}
In this way, the datetimepicker display is empty during program initialization.
However, there is a problem with this writing method. When saving it to the database, you must add a judgment if (this. datetimepicker1.text. tostring () = ""), save null value; else save this. datetimepicker1.value.
This method has encountered a bug that has not been solved. By default, the date control is empty. After you select a date for the first time, you must lose the focus to select a new date. Why?
Method 3:
Overwrite a text box on the date control, and the text box is null at the time of initialization. After each date is selected, the value is attached to the text box.
Http://hi.baidu.com/pollywog/blog/item/8ec254cecfd33731b600c856.html