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;
               }
                You just need to set property valueDefault={null} and the DatePicker will be blank.
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.
Use the following code:
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
                        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;
}
                        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;
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With