14/12/2013 by Nitesh

How To Set DateTimePicker controls value blank in C#

Friends,

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