I have this form right here, where the user will add the start date of the diagnosis. pretty simple right? but in the end
diagnosis how would I set it to a blank? or 0 value? here's my form.

- 15,637
- 61
- 177
- 270
-
Could you show the code? Hint though would be to use Nullable
DateTime? so it can store null. DateTime is a value object and therefore cannot store null – James Kyburz Mar 25 '12 at 14:21 -
In my database, the data type is only date. I mean in the UI how would set their initial values to null or 0? it will have a value if the user clicks it. – user962206 Mar 25 '12 at 14:23
-
possible duplicate of [DateTimePicker Null Value (.NET)](http://stackoverflow.com/questions/284364/datetimepicker-null-value-net) – bluish Nov 13 '13 at 08:38
7 Answers
The best you can do is to set ShowCheckBox
to true. If the CheckBox
is not checked, consider that no end date is set.

- 286,951
- 70
- 623
- 758
-
-
No, the `Value` property is still set to a valid date. You need to test the `Checked` property to decide if you want to use the date or not. – Thomas Levesque Mar 25 '12 at 14:28
i think, it is not possible to set it as blank or 0 value.. the least you can do is to set it in its MinDate if it's null..
this one works for me..
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";

- 15,750
- 31
- 68
- 83

- 21
- 1
you don't want it to be set to 0. It makes no sense (1/1/01 ? most of DB doesn't event store this date)
You should put it either the same as start or in a reasonable margin which describe the common scenario (like + 2 weeks)

- 14,639
- 11
- 62
- 110
try to use
dateTimePicker1.Value.Date
It would return you only date

- 1,640
- 4
- 31
- 52
-
1This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Qantas 94 Heavy Nov 24 '13 at 00:13
All solved now just had to adjust. As what you guys were saying i wasn't setting the value or the properties so it would all ways be null anyway
private string _BirthDate;
public Form1()
{
InitializeComponent();
dateTimePicker1.MaxDate = DateTime.Now; // Ensures no future dates are set.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
//dateTimePicker1.Checked = true;
dateTimePicker1.CustomFormat = dateTimePicker1.CustomFormat;
dateTimePicker1.Value = DateTime.Now;
}
public string BirthDate
{
set
{
if (dateTimePicker1.Value.Date == DateTime.Today)
{
dateTimePicker1.CustomFormat = " ";
}
else
{
_BirthDate = value;
}
}
get { return _BirthDate; }
}
private void button1_Click(object sender, EventArgs e)
{
BirthDate = dateTimePicker1.Value.ToString();
}

- 633
- 1
- 6
- 14
Try setting the Value property. However I think there is a limit on how far back you can set the value.

- 9,636
- 2
- 33
- 46
-
I want the datetime picker to display blank. or a null value how can I achieve that? – user962206 Mar 25 '12 at 14:27
-
You cant null the date value you could only set it to a date far in the past. See Thomas Levesque's answer about placing a check box next to the date, that might get you what you want. – Devin M Mar 25 '12 at 14:29
You can put an external checkbox and disable the datetimepicker when you wish to signify no-date.

- 768
- 1
- 5
- 13