RadDatePicker and Null values

13 posts, 1 answers
  1. Dean Allan
    Dean Allan avatar
    12 posts
    Member since:
    Jan 2010

    Posted 20 Feb 2011 Link to this post

    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.
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Feb 2011 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Dean Allan
    Dean Allan avatar
    12 posts
    Member since:
    Jan 2010

    Posted 20 Feb 2011 Link to this post

    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.
  5. Answer
    Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 23 Feb 2011 Link to this post

    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!
  6. Dean Allan
    Dean Allan avatar
    12 posts
    Member since:
    Jan 2010

    Posted 28 Feb 2011 Link to this post

    Thanks everyone!
  7. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 28 Feb 2011 Link to this post

    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
  8. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 02 Mar 2011 Link to this post

    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
  9. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 02 Mar 2011 Link to this post

    I also plan to use your sample Emanuel. Thanks
    Richard
  10. John
    John avatar
    37 posts
    Member since:
    Jun 2010

    Posted 03 Mar 2011 Link to this post

    Link does not work.. can you please repost
  11. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 03 Mar 2011 Link to this post

    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
  12. hartley
    hartley avatar
    9 posts
    Member since:
    Nov 2009

    Posted 03 Aug 2011 Link to this post

    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;
  13. Kholisa
    Kholisa avatar
    10 posts
    Member since:
    May 2012

    Posted 17 May 2012 Link to this post

    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
  14. Marco
    Marco avatar
    87 posts
    Member since:
    Apr 2011

    Posted 23 Aug 2012 Link to this post

    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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready