'How to set null value to Datetimepicker when load?
I have a Datetimepicker1, i want to set it to empty when load, and show value after chose.
I have found a code
Datetimepicker1.Format = DateTimePickerFormat.Custom
Datetimepicker1.CustomFormat = " "
but it does not show value after chose.
Solution 1:[1]
Try this
DateTime? dateSample= null;
Solution 2:[2]
DateTimePickers can't be set to null because DateTime can't be null, but you can set them to DateTime.MinValue which is the default value for an uninitialized DateTime. And then you just check if the dtp.Value = DateTime.MinValue and if so, treat it as null.
However, if you want to really distinguish when no value has been selected, the easiest way is to set DateTimePicker.ShowCheckBox to true, and then you check dtp.Checked and if it's true, you read the value, otherwise you treat it as a null.
Have you try this : for testing purpose
DateTime? myselectedDate = null;
private void DateTimePicker1_ValueChanged(Object sender, EventArgs e) {
myselectedDate = DateTimePicker1.Value;
}
And
this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.CustomFormat = " "; //a string with one whitespace
Solution 3:[3]
DatetimePicker can't have no value. Null or default value is DateTime.MinValue. Your code changes the format in order to show nothing.
To show the date again, simply change DateTimePicker.Format again when needed:
DateTimePicker1.Format = DateTimePickerFormat.Long;
Solution 4:[4]
Because , you formatted your datetimepicker "", you should write the below code in valuechanged event of the datetimepicker;
Datetimepicker1.Format = DateTimePickerFormat.short(type which you need short,long etc..)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AttaKhan |
| Solution 2 | |
| Solution 3 | SysDragon |
| Solution 4 | ibrahim hulisi ergüler |
