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

RadDateTimePicker - Setting Selected Value To Today?

7 Answers 1494 Views
Calendar, DateTimePicker, TimePicker and Clock
This is a migrated thread and some comments may be shown as answers.
IT
Top achievements
Rank 1
IT asked on 22 Jun 2016, 09:55 AM

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

7 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 22 Jun 2016, 03:24 PM
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.
0
Nick
Top achievements
Rank 1
answered on 05 Oct 2016, 04:29 PM

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.

0
Hristo
Telerik team
answered on 06 Oct 2016, 03:34 PM
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.
0
Nick
Top achievements
Rank 1
answered on 06 Oct 2016, 05:27 PM

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.

0
Hristo
Telerik team
answered on 07 Oct 2016, 02:00 PM
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.
0
Nick
Top achievements
Rank 1
answered on 21 Dec 2016, 05:09 PM

Hello Hristo. 

I finally solved this by using these two lines whenever I load a form with a RadDateTimePicker control.

rdtpSomeDate.DateTimePickerElement.Calendar.RemoveFocusedDate(True)
rdtpSomeDate.DateTimePickerElement.Calendar.FocusedDate = DateTime.Now

Nick.

0
Hristo
Telerik team
answered on 22 Dec 2016, 09:12 AM
Hi Nick,

Thank you for writing back.

I am glad that you have managed to achieve the desired result. Thank you also for sharing your solution with the community.

Please let me know if you need further assistance.

Regards,
Hristo
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Calendar, DateTimePicker, TimePicker and Clock
Asked by
IT
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Nick
Top achievements
Rank 1
Share this question
or