C # Date Control DateTimePicker Save Null method

Source: Internet
Author: User

Method One (recommended):

Set DateTimePicker Property ShowCheckBox to True

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

When you save a date value in storage, you can save the null value according to if (this.datetimepicker1.Checked ==false).

Method Two:

In the window initialization function, add:
This.datetimepicker1.format=datetimepickerformat.custom;
This.datetimepicker1.customformat= "";
In the date Change event, write:
private void Datetimepicker1_valuechanged (object sender, System.EventArgs e)
{
This.datetimepicker1.format=datetimepickerformat.long;
This.datetimepicker1.customformat=null;
}
This enables the DateTimePicker to appear empty when the program is initialized

However, this writing has a problem, when you save the storage, you have to add a judgment if (this.dateTimePicker1.Text.toString () = = ""), Save the empty value; else save This.dateTimePicker1.value.

This writing encountered a bug, has not been resolved, is the date control is empty by default, after the first selection of a date must lose focus to select a new date, do not know why?

Method Three:

Overlay a text box above the date control, and then initialize the text box with a null value, attaching the value to the text box after each date selection.

C # Date Control DateTimePicker Save Null method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.