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

DateTimePicker crash

3 Answers 395 Views
Calendar, DateTimePicker, TimePicker and Clock
This is a migrated thread and some comments may be shown as answers.
Olli
Top achievements
Rank 1
Olli asked on 09 Oct 2012, 02:42 PM
how can i avoid DateTimePicker crashing?
when I set DateTimePicker to minimum value of 01.01.0001 (ex. when you set it to Null value) and then open calendar (positioned at 01.01.0001) and move a month (or a year) earlier - application crashes.

when it's a simple DateTimePicker i can set a minimum value and a null value to secure the situation.
but when DateTimePicker is a part of RadGridView column type of DateTime, i have no ability of setting values of Minimum or Null for it. 

do you have any solutions for this situation?

3 Answers, 1 is accepted

Sort by
0
Accepted
Peter
Telerik team
answered on 10 Oct 2012, 03:49 PM
Hello Olli,

Thank you for writing.

You can access the DateTime editor in the CellEditorInitialized event, for example:

void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    RadDateTimeEditor editor = e.ActiveEditor as RadDateTimeEditor;
    if (editor != null)
    {
  
        RadDateTimeEditorElement element = editor.EditorElement as RadDateTimeEditorElement;
        element.MinDate =...;
        element.NullDate=...;
    }
}
    

I hope this helps. All the best,
Peter
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Olli
Top achievements
Rank 1
answered on 11 Oct 2012, 08:18 AM
hi, Peter!
thanks a lot for your code - it works perfectly!

as far as i am a little bit lazy, and the project has a set of gridviews with the same requirements, i created an extension class based on your example:

public static class Extensions
{
public static DateTime DatePickerMinDate = new DateTime(1900, 01, 01);
public static DateTime DatePickerNullDate = DatePickerMinDate;
public static void SetDateRestriction(this RadGridView theGrid)
{
theGrid.CellEditorInitialized -= theGrid_CellEditorInitialized;
theGrid.CellEditorInitialized +=new GridViewCellEventHandler(theGrid_CellEditorInitialized);
}

static void theGrid_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
  RadDateTimeEditor editor = e.ActiveEditor as RadDateTimeEditor;
if (editor != null)
{
RadDateTimeEditorElement element = editor.EditorElement as RadDateTimeEditorElement;
element.MinDate = DatePickerMinDate;
element.NullDate = DatePickerNullDate;
}
}
}


so the only thing left to do is just attaching date restrictions to the required editable gridview having datetime cells - it is easily done by calling extension method after initializing control, for example:

public partial class PaymentHistoryView: UserControl 
{
public PaymentHistoryView()
{
InitializeComponent();

PaymentHistoryGridView.SetDateRestriction();
}

// ...
}

the realization is not very flexible, but it meets all current requirements and it can be upgraded easily if needed.

thanks again for your help.
0
Peter
Telerik team
answered on 12 Oct 2012, 01:18 PM
Hi Olli,

Thank you for sharing your solution with the community. I am sure that someone will benefit from it.

Greetings,
Peter
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
Tags
Calendar, DateTimePicker, TimePicker and Clock
Asked by
Olli
Top achievements
Rank 1
Answers by
Peter
Telerik team
Olli
Top achievements
Rank 1
Share this question
or