SoFunction
Updated on 2025-03-07

Three ways to save null values ​​by datepicker in C# date control

Method 1 (recommended):

Set the datetimepicker property ShowCheckBox to true

When the window is initialized, add the code this. = false;

When saving the date value into the database, you can save the empty value according to if(this. ==false).

Method 2:

Add in the window initialization function:

Copy the codeThe code is as follows:

this.=;

this.="   ";


Write in a date change event:
Copy the codeThe code is as follows:

private void dateTimePicker1_ValueChanged(object sender, e)

{

this.=;

this.=null;

}


This is achieved, the dateTimePicker is displayed empty when the program is initialized

However, there is a problem with this writing method. When saving and entering the database, you need to add a judgment if(this.()=="") and save the empty value; else save this.

This writing method encountered a bug, but it has not been solved. The date control is empty by default. After selecting a date for the first time, you must lose focus to select a new date. I wonder why?

Method 3:

Overwrite a text box on the date control, and then the text box is empty when initialized. After each date selection, the value is attached to the text box.