December 14, 2013 by Nitesh

How To Set DateTimePicker controls value blank in C#


We all have been using the default DateTimePicker control in C# Winforms and in certain scenarios, we need to display a blank value in the DateTimePicker control. By default you can set the control’s value to null or DBNUll.Value. However, this can be done by using a small trick. This post will explain how you can set the DateTimePicker control’s value to blank at the time of display.

Let us assume the name of our DateTimePicker control is “dtpDate”. To display a blank value on this contrl, write the below lines of code in your Form_Load()

dtpDate.Format = DateTimePickerFormat.Custom;
dtpDate.CustomFormat = " ";

To understand the above lines, we are first setting the format of the DateTimePicker Control to “Custom” format. Once the format type is set, we set a format text as a blank space to the DateTimePicker control. Once this is done, the DateTimePicker control has a blank value displayed on the form.

Hope you like this small tip. In case you know any alternative way of doing the same, share it via comments. Cheers!

#C##How To?#Troubleshooting#Winforms
  • Laire

    But then you cant select a date using that blank date time picker.

    • Nitesh Luharuka

      You can still select the date by using the calendar control on the datetime picker.

      • Paul E. Kahl

        Actually, no. You can’t. The second you change the format to the single space string, the field becomes un-editable. You can’t tab to it, or click on it, and are thus unable to edit it.

        • Can you share a screenshot of what you see. I double checked and it works.

Support us!

If you like this site please help and make click on any of these buttons!

Powered by WordPress Popup