DateTimePicker crash

4 posts, 1 answers
  1. Olli
    Olli avatar
    2 posts
    Member since:
    Oct 2012

    Posted 09 Oct 2012 Link to this post

    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?
  2. Answer
    Peter
    Admin
    Peter avatar
    1148 posts

    Posted 10 Oct 2012 Link to this post

    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 >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Olli
    Olli avatar
    2 posts
    Member since:
    Oct 2012

    Posted 11 Oct 2012 Link to this post

    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.
  5. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 12 Oct 2012 Link to this post

    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.
Back to Top