Tuesday, October 1, 2013

How to make DatePicker control as null or empty in Windows Application?


This is common problem with DatePicker control - We cannot assign null or empty value to its value property. In this case, showing DatePicker control as empty is bit difficult. I have done one work around for this and now I am able to show empty DatePicker control.

In this article, I will explain - How to make DatePicker control as null or empty?

Check the below code implementation.

Workaround-

To make it possible, I have used "CustomFormat" property of DatePicker control.

//To set DatePicker Control format as "MM/yyyy"
private void dtPickerDOB_ValueChanged(object sender, EventArgs e)
{
   dtPickerDOB.Format = DateTimePickerFormat.Custom;
   dtPickerDOB.CustomFormat = "MM/yyyy";
}


In above code, I am setting CustomFormat = "MM/yyyy". So the date will be displayed like this- "10/2013"

Now to make this DatePicker cotrol as empty-

//To make DatePicker Control as empty
private void btnClearDate_Click(object sender, EventArgs e)
{
  // Setting CustomFormat as Space.           
  dtPickerDOB.Format = DateTimePickerFormat.Custom;
  dtPickerDOB.CustomFormat = " ";




In above code, I have set CustomFormat = " " so empty value will be displayed in DatePicker control.

Try this code


Thanks

No comments:

Post a Comment