RadDateTimePicker - Setting Selected Value To Today?

6 posts, 0 answers
  1. Shane
    Shane avatar
    9 posts
    Member since:
    Feb 2016

    Posted 22 Jun Link to this post

    I've got a few RadDateTimePickers which are all bound to various data sources. Some of them have a null value, some have values, which all display fine - however the issue I am having is that when you click the field to select a date, the default date that is selected is from when I added the control to the form.

     

    This should be today's date, however the value of the field should remain null until the user has clicked a date. They do need to be able to select a date in the past/future though as well.

     

    I'm using the latest version of Telerik UI for WinForms on a VB.net project.

     

    Any help would be much appreciated.

     

    Thanks, Shane

  2. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 22 Jun Link to this post

    Hi Shane,

    Thank you for writing.

    You can set the value of the editor to null  by calling its SetToNullValueSet Null or Empty Value.

    The control also defines MinDate and MaxDate properties with which you would be able to set range with allowed dates. Please check my code snippet below: 
    Public Sub New()
        InitializeComponent()
     
        Me.radDateTimePicker1.NullText = "No date selected"
        Me.radDateTimePicker1.NullableValue = Nothing
        Me.radDateTimePicker1.SetToNullValue()
     
        Me.radDateTimePicker1.MinDate = DateTime.Now.AddDays(-20)
        Me.radDateTimePicker1.MaxDate = DateTime.Now.AddDays(20)
    End Sub

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Nick
    Nick avatar
    2 posts
    Member since:
    Aug 2016

    Posted 05 Oct in reply to Shane Link to this post

    Shane, did you solve this issue.  Hristo seems to have glossed over what you actually asked and posted about how to use the NullableValue property.  I now have the same issue as you described...

    A RadDateTimePicker is bound to a database date column that can have a Null value.  Properties as displayed in VS2015:

    DataBindings.Value = [BindingSource - ColumnName]
    NullText = "No date selected"
    Text = [the date I added the control to the form]
    Value = [the date/time I added the control to the form]

    When run and the database value is Null, the control correctly displays "No date selected".  When the user clicks the dropdown to choose a date from the calendar, the default date is the date the control was added to the form.

    What I need, and I think you also wanted, is the calendar by default (when the underlying value was previously Null), to display the current date and not the date when the control was added to the form at design-time.

    Is there a way at run-time to set this default date?

     

    Nick.

  5. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 06 Oct Link to this post

    Hello Nick,

    Thank you for clarifying the scenario.

    When you add a RadDateTimePicker from the toolbox its Text and Value are serialized. Setting the value property at the end also updates the selected dates of the calendar. 

    In this type of scenario, it is appropriate to set the FocusedDate property to the required date this way have it displayed in the calendar drop down:
    this.radDateTimePicker1.DateTimePickerElement.Calendar.FocusedDate = DateTime.Now;

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
  6. Nick
    Nick avatar
    2 posts
    Member since:
    Aug 2016

    Posted 06 Oct Link to this post

    Hello Hristo.  Thanks for the reply.

    Setting DateTimePickerElement.Calendar.FocusedDate=Now didn't change anything.  The control still shows the date it was added to the form.  According to the radCalendar properties page setting FocusedDate should do exactly what's required.  Sadly, as implemented as part of radDateTimePicker it doesn't appear to work. 

    I also looked at DateTimePickerElement.Calendar.RemoveFocusedDate.  According to the documentation, it should remove the focused date and change the current view to today.  That did remove the highlighted day, but kept the same original month displayed, not the current month.  That happened even if I set FocusedDate straight afterwards.

    Nick.

  7. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 07 Oct Link to this post

    Hello Nick,

    Thank you for writing.

    I performed some testing with the following setup and on my end, the FocusedDate is correctly displayed in the calendar. Can you please check how it runs on your end?
    public partial class Form1 : Form
    {
        private BindingList<DateObject> data;
     
        public Form1()
        {
            InitializeComponent();
     
            data = new BindingList<DateObject>();
            data.Add(new DateObject(null));
     
            this.radDateTimePicker1.DataBindings.Add(new Binding("NullableValue", this.data, "Date", true, DataSourceUpdateMode.OnPropertyChanged));
            this.radDateTimePicker1.NullText = "No date selected";
     
            this.radDateTimePicker1.DateTimePickerElement.Calendar.FocusedDate = DateTime.Now;
        }
        public class DateObject
        {
            private DateTime? date;
     
            public DateObject(DateTime? date)
            {
                this.date = date;
            }
     
     
            public DateTime? Date
            {
                get { return date; }
                set { date = value; }
            }
        }
    }

    In case you keep experiencing the issue please open a support ticket and send us your project.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready