Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting DateTimePicker to Null

I am unable to set the set the Datetimepicker to Null, how can do it. In my project my requirement is to validate the DTPif it is a null, for that I need to set to Null, The code I am using is:

              {
                dateInsert.Format = DateTimePickerFormat.Custom;
                dateInsert.Text = string.Empty;
               }
like image 624
user2749421 Avatar asked Sep 12 '13 05:09

user2749421


People also ask

How do you make a DatePicker empty?

You just need to set property valueDefault={null} and the DatePicker will be blank.

How check DatePicker is null?

The DatePicker class has a property, SelectedDate, which enable you to get or set the selected date. If there is none selected, it means its value will be null.


3 Answers

Use the following code:

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
like image 131
karthik reddy Avatar answered Sep 22 '22 12:09

karthik reddy


Take a variable and set its value when DateTimePicker value changes

e.g.

DateTime? myselectedDate = null;

private void DateTimePicker1_ValueChanged(Object sender, EventArgs e) {
   myselectedDate = DateTimePicker1.Value;
}
like image 40
Ronak Patel Avatar answered Sep 20 '22 12:09

Ronak Patel


I had a similar question but I could not find an answer I liked. However, I did come up with a suitable solution. Set the "ShowCheckBox" property for the datetimepicker to true. This will put a check box next to the date in the box. Then add the following code.

if (dateTimePicker1.Checked == false) 
    myVariable = ""; 
else 
    myVariable = dateTimePicker1;
like image 27
Tim Avatar answered Sep 21 '22 12:09

Tim