Set DateTimePicker value to be null
Set DateTimePicker value to be null
I am developing this UI. I have 2 DateTimePicker controls. Initially i want to set the value of the controls to be null, until a user selects a date and hence if user do not select the date, it will pass the value as null to the data base. By default it takes current date. Can you plz suggest something. or attach a piece of code which will help me out. I have tried setting the MinDate property, it doesn't work. The code is in C# .net.
Thanks, Manali.
Answer by JTorrecilla for Set DateTimePicker value to be null
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.
Answer by V4Vendetta for Set DateTimePicker value to be null
I hope this should help you Link1 To display blank in the picker field
and this too Link2
Answer by Manali for Set DateTimePicker value to be null
To display the null value in DateTimePicker
control, Make the following changes in the Designer.cs
file:
this.DateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom; this.DateTimePicker.CustomFormat = " ";
When user selects a date, code-in the ValueChanged
property of the control and make the format according to your needs. This will display the date in the control.
The above one only display's the control as blank in UI, however the default value will still be the current date.
If you want to set/initialize the value as null or empty string:
- declare a temporary string variable say temp
temp = DateTimePicker.CustomFormat.ToString();
- check whether temp is an empty string. If so handle as you like.
This is the easiest workaround I found.
Answer by K Kimble for Set DateTimePicker value to be null
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.
// Use ValueChanged to decide if the value should be displayed: dateTimePicker1.ValueChanged += (s, e) => { dateTimePicker1.CustomFormat = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? "MM/dd/yyyy" : " "; }; //When getting the value back out, use something like the following: DateTime? dt = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? (DateTime?) dateTimePicker1.Value : null; // or DateTime dt2 = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? dateTimePicker1.Value : DateTime.MinValue;
Answer by Zyo for Set DateTimePicker value to be null
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
if (value == DateTime.MinValue) { datePicker.Checked = false; } else { datePicker.Checked = true; datePicker.Value = value; }
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.
if (!datePicker.Checked) { // hide date value since it's not set datePicker.CustomFormat = " "; datePicker.Format = DateTimePickerFormat.Custom; } else { datePicker.CustomFormat = null; datePicker.Format = DateTimePickerFormat.Long; }
Answer by Sameer Khan for Set DateTimePicker value to be null
dateTimePicker1.CustomFormat = " ";
Answer by Bhaskar Jain for Set DateTimePicker value to be null
Let suppose dtpStartDate
is your DateTimePicker control.
Write this code in Page_Load-
`dtpStartDate.Format = DateTimePickerFormat.Custom; dtpStartDate.CustomFormat = " ";`
Write this code in Value_Changed event-
`startDate = Convert.ToString(dtpStartDate.Value); dtpStartDate.Format = DateTimePickerFormat.Short;`
here selected 'short' as Format. You can select 'Long' and other option available.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment