This is a migrated thread and some comments may be shown as answers.

RadDatePicker and Null values

14 Answers 2730 Views
Calendar, DateTimePicker, TimePicker and Clock
This is a migrated thread and some comments may be shown as answers.
Dean Allan
Top achievements
Rank 1
Dean Allan asked on 20 Feb 2011, 09:19 AM
Hello,

I've tried to find some info on this but I'm not sure what the problem really is.  Overall I have a master/detail record situation.

I have a datasource bound to a RadGrid and to form items in a "detail" view above the grid. When I click a row the form items in the detail view update with the current record data.  Works great, drop down's and all.  However, the RadDateTime picker has me puzzled.  It updates fine until it hits a record with a null value for the date. Rather than going blank it holds the last date that it received. I expected it to go blank on a null value.  The datepicker supports a 'work order closed' field and will remain null until some point in the future.

Is there something i'm missing? or am I going against best practice and there is a better way?

Thanks,

Dean.

14 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 20 Feb 2011, 01:06 PM
Hi Dean,

Because the RadDateTimePicker only deals in dates, the value itself cannot be null. You can set a null date, and a null text to show that you intend it to be null.

So, let's say you have a RadDateTimePicker, and a RadButton on a form. You can set the null value properties like this
Me.RadDateTimePicker1.NullText = "No Date"
Me.RadDateTimePicker1.NullDate = New Date(1980, 1, 1)

This means that when you set the RadDateTimePicker to null date, it will set the value to 1st Jan 1980, and the text to "No Date"

In my exmaple then, you initially set the value to today.
Me.RadDateTimePicker1.Value = DateTime.Now()

and then on click of the button you can set it to null value
Me.RadDateTimePicker1.SetToNullValue()

You would end up with the RadDateTimePicker having a value of 1st Jan 1980 and the text being "No Date"
Hope you find that helpful.
Richard
0
Dean Allan
Top achievements
Rank 1
answered on 21 Feb 2011, 05:48 AM
Hey Richard,

Thanks for the info!

I created a test app and I see how setting the Null date of the DatePicker will show empty if the database contains the same value, ie. 1/1/1900. 

However, It would appear that i I'm forced to setting all my code to reflect 1/1/1900 as null. Not to mention all other controls show 1/1/1900 rather than null (empty), like text boxes and the radgrid.  I also realize this is not limited to the Rad DatePicker, the MS Datepicker works the same way. Setting the Nulldate to Null would be a solution.

What I am looking for is something like we had in ASP. A button that acted as a calendar and inserted the chosen date into a text box; empty date, empty text box.   I found this article, so I think there is hope.  http://www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c9645

I think I'll just switch to a text box for now.

Thanks again,

Dean.
0
Accepted
Stefan
Telerik team
answered on 23 Feb 2011, 10:53 AM
Hello guys,

Thank you for the discussion.

I am writing to confirm that currently RadDateTimePicker does not support null dates and the only way to work with it is the one that Richard has suggested. 

However, your suggestion seems quite reasonable and for this reason I will add it to our PITS system as a feature request. At this link you can subscribe to the status updates of the issue.

@Dean Allan, I have updated your Telerik points for this request.

Should you have any other questions, do not hesitate to contact me.
 
Regards,
Stefan
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Dean Allan
Top achievements
Rank 1
answered on 28 Feb 2011, 06:59 AM
Thanks everyone!
0
Emanuel Varga
Top achievements
Rank 1
answered on 28 Feb 2011, 10:48 AM
Hello guys,

I have written a very basic NullableDateTimePicker using the checkbox of the original datetime picker.

Please take a look at the attached screenshots.

I will submit the project to the code projects library today.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Emanuel Varga
Top achievements
Rank 1
answered on 02 Mar 2011, 03:18 PM
Hello guys,

If anyone is interested i have posted the NullableDateTimePicker code library project here.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Richard Slade
Top achievements
Rank 2
answered on 02 Mar 2011, 03:20 PM
I also plan to use your sample Emanuel. Thanks
Richard
0
John
Top achievements
Rank 1
answered on 03 Mar 2011, 09:46 PM
Link does not work.. can you please repost
0
Richard Slade
Top achievements
Rank 2
answered on 03 Mar 2011, 10:08 PM
Hi John,

Emanuel's Code Library article was moved into the Editors section of the Code Library and can now be found here
All the best
Richard
0
hartley
Top achievements
Rank 1
answered on 03 Aug 2011, 09:51 PM
I have come up with a solution to handle a null DateTime value when Data Binding a RadDateTimePicker. I am using Winforms Q1 2011 and a BindingSource in C# .NET 3.5.

After looking at various discussions on how to handle null values with date pickers (1, 2, 3 , 4 ), I came across the MSDN article, Improvements to Windows Forms Data Binding, and the Automatic Handling of Null and DBNull Values section (link).  

Now, during control initialization I add the Binding.NullValue property (link), and change the RadDateTimePicker's NullDate, NullText, and Value properties.

The Binding object NullValue and the RadDateTimePicker NullDate are set to equal values. So, when null is retrieved by the Binding object, it is defaulted to the same value as the RadDateTImePicker's NullDate. When the user deletes the value on the form, the control converts it into the NullDate and passes that to the Binding object. The Binding object converts it from a date to Null and passes it to the DataSource.

Todays date is selected and displayed in the RadDateTimePicker by off setting the NullDate 1 second compared to the Value. The Data Binding will still set the correct date in the control even though the Value property is being set.

The code below is from the InitializeComponent() of a detail form that loads one record. The radDateTimePicker is bound to a BindingSource with a strongly typed Dataset connected to SQL Server.  I have made changes by copying the default line and modifying it on the line below.

//
// radDateTimePicker1
//
this.radDateTimePicker1.Culture = new System.Globalization.CultureInfo("en-CA");
//this.radDateTimePicker1.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.tblClientBindingSource, "ClientSinceDate", true));
this.radDateTimePicker1.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.tblClientBindingSource, "ClientSinceDate", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, System.DateTime.Now.Date.AddSeconds(1), ""));
this.radDateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Long;
this.radDateTimePicker1.Location = new System.Drawing.Point(491, 160);
this.radDateTimePicker1.MaxDate = new System.DateTime(9998, 12, 31, 0, 0, 0, 0);
this.radDateTimePicker1.MinDate = new System.DateTime(1753, 1, 1, 0, 0, 0, 0);
this.radDateTimePicker1.Name = "radDateTimePicker1";
//this.radDateTimePicker1.NullDate = new System.DateTime(2006, 1, 1, 0, 0, 0, 0);
this.radDateTimePicker1.NullDate = System.DateTime.Now.Date.AddSeconds(1);
this.radDateTimePicker1.NullText = "";
this.radDateTimePicker1.Size = new System.Drawing.Size(200, 20);
this.radDateTimePicker1.TabIndex = 71;
this.radDateTimePicker1.TabStop = false;
this.radDateTimePicker1.Text = "radDateTimePicker1";
//this.radDateTimePicker1.Value = new System.DateTime(2011, 8, 3, 11, 19, 57, 978);
this.radDateTimePicker1.Value = System.DateTime.Now.Date;
0
Kholisa
Top achievements
Rank 1
answered on 17 May 2012, 09:47 AM
Hi There

How do i set my rad date picker to accept null values on client event? i want it to commit null on the database when not selected by a user

Thanks
0
Marco
Top achievements
Rank 2
Veteran
answered on 23 Aug 2012, 07:14 AM
I think that's the best way to manage a nullable date with datatimepicker is to handle the format and parse event of the binding.

You choose a specifique date that's represent the null date (1/1/1980). Be sure that's this date will never be used in your application (Depending on your buisness environnement)!

Set the nulldate and the nulltext on your date time picker.

Use a binding "with event" and add a handle on the format and parse event (you can reuse the same function for all the null date time picker).

On the format event, check if the datasource is a null value (could be nothing, could be DBNull.Value, something else, it's all depending your data layer (ADO, Entity Framework, usw.) In case of sucess, send to the datetimepicker the special null date (1/1/1980) instead of the datasource value.
On the parse event, check if the datetimepicker has send the special null date (1/1/1980). In case of sucess, send to your datasource a real null value (Nothing, new null datetime,...).

These tricks works really good for displaying null value. If you need to reset the date to null, you should consider to add a small button for this job. Because your user won't understand that's they had to select a specifique date for choosing nothing...

You have to declare and make these kind of binding on the code page, not using the designer!

By the way, you could also use the binding event tricks to display and handle null value in a combobox !

It could be a good idea to add a chapter, with a small exemple, about handing null value to the doc.
0
Gh Reza
Top achievements
Rank 1
answered on 26 Apr 2020, 11:14 AM

Hi ,

I'm using radDateTimePicker 2020 Q1, and Set it's properties as

.NullText = "";

.NullableValue = null;
.SetToNullValue(); 

at design time.

My problem is when user want to clear his/her input datetime (Null radDateTime Value) without add any extra button to my form.

0
Nadya | Tech Support Engineer
Telerik team
answered on 29 Apr 2020, 09:44 AM

Hello Gh Reza,

I suppose that you want to clear the value in RadDateTimePicker when some key is pressed. If you do so please refer to the following code snippet:

private void radDateTimePicker1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Back)
            {
                radDateTimePicker1.SetToNullValue();
            }
        }

I hope this helps. Should you have other questions do not hesitate to ask.

Regards,
Nadya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Calendar, DateTimePicker, TimePicker and Clock
Asked by
Dean Allan
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Dean Allan
Top achievements
Rank 1
Stefan
Telerik team
Emanuel Varga
Top achievements
Rank 1
John
Top achievements
Rank 1
hartley
Top achievements
Rank 1
Kholisa
Top achievements
Rank 1
Marco
Top achievements
Rank 2
Veteran
Gh Reza
Top achievements
Rank 1
Nadya | Tech Support Engineer
Telerik team
Share this question
or