I am developing a WinForms UI with two DateTimePicker
controls. Initially I want to set the value of the controls to null until a user selects a date. If the user does not select a date, it will pass a value of null to the database.
By default it takes the current date.
Could you plz suggest something or attach a piece of code which will help me out.
I have tried setting the MinDate
property but it doesn't work.
The code is in C# .Net.
Thanks, Manali.
I hope this should help you Link1 To display blank in the picker field
and this too Link2
I know this is an older post, but others might still find this useful. It is fairly elegant and concise. I used this with a .Net 4.0 DateTimePicker but earlier versions should work with minimal tweaking. It leverages MinDate and Checked.
May be you need to create a UserControl withe the Appereance of a TextBox,and when the user click on a Calender Show, when you select a Date set it to the TextBox. It will allow nulls values.
The DateTimePicker does not allow null.
This is the code of a nullable datetime picker that I use:
I think the best solution is to use the build-in checkbox that tell the user if a value is specified or not.
Set the control property
ShowCheckBox = true
When you bind the value to it do something like
When reading back the value check the
Checked
property.If you don't like the displayed value when it's unchecked, you can combine that with the other suggestions.