RadDateTimeEditor How to Type YYYYMMDD

4 posts, 1 answers
  1. shao
    shao avatar
    3 posts
    Member since:
    Sep 2014

    Posted 11 Sep 2014 Link to this post

    I have the following question, can you help me to work it out?
    Clients have the following requirements.
    In RadDateTimeEditor, it can be typed by 'YYYYMMDD'.
    But now the default thing is It has to be typed by 'YYYY/MM/DD'.
    So, Is there any way to do that.
    Thanks
  2. Ralitsa
    Admin
    Ralitsa avatar
    176 posts

    Posted 15 Sep 2014 Link to this post

    Hi Shao, 

    Thank you for contacting us. 

    I guess that you use RadGridView with GridViewDateTimeColumn. If you want to type date in another format, you need to subscribe to the CellEditorInitialized event and change the format. Please take a look at the following code snippet: 
    void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadDateTimeEditor editor = this.radGridView1.ActiveEditor as RadDateTimeEditor;
     
        if (editor != null)
        {
            if (e.Column.Name == "DateTimeColumn")
            {
                editor.CustomFormat = "yyyyMMdd";
            }
        }
    }

    If I did not understand correctly your case, please describe with more details what you want to achieve and what control you use from our suit. 

    Let me know if you have any other questions.

    Regards,
    Ralitsa
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. shao
    shao avatar
    3 posts
    Member since:
    Sep 2014

    Posted 16 Sep 2014 in reply to Ralitsa Link to this post

    Big thank to your reply.
    I am so sorry for that I might have you confused about my question.

    The special requirements is :
    the end user wants to directly type a value [20140916] in the RadDateTimeEditor of GridViewDateTimeColumn,
    and he doesn't want to to type year portion after year is typed. the same is month portion or day portion.

    Specifically, After he types '2014', the cursor is still in the year. And when he wants to type 09 month, the year is changed to 1409.

    Is there any way for end user to skip the portion typing? And after he types 20140916, then the value is automatically changed to '2014/09/16'.

  5. Answer
    Ralitsa
    Admin
    Ralitsa avatar
    176 posts

    Posted 18 Sep 2014 Link to this post

    Hi Shao, 

    Thank you for your reply. 

    This is not supported and to move to the next segment you should either use the arrow key or just press forward slash key. Nevertheless you can implement such functionality if it is a must for your application. 
    For example you can use the KeyUp event of the text box item:
    private int day = 0;
    private int month = 0;
    private int year = 0;
     
    void TextBoxItem_KeyUp(object sender, KeyEventArgs e)
    {
        if ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) || e.KeyCode == Keys.Decimal)
        {
            RadTextBoxItem item = sender as RadTextBoxItem;
            RadMaskedEditBoxElement element = item.Parent as RadMaskedEditBoxElement;
            MaskDateTimeProvider provider = element.Provider as MaskDateTimeProvider;
     
            switch (provider.List[provider.SelectedItemIndex].type)
            {
                case PartTypes.Day:
                    day += 1;
                    break;
                case PartTypes.Month:
                    month += 1;
                    break;
                case PartTypes.Year:
                    year += 1;
                    break;
            }
     
            if (year == 4)
            {
                provider.SelectNextEditableItem();
                year = 0;
            }
            if (month == 2)
            {
                provider.SelectNextEditableItem();
                month = 0;
            }
            if (day == 2)
            {
                provider.SelectNextEditableItem();
                day = 0;
            }
        }
        else if (e.KeyData == Keys.Left || e.KeyData == Keys.Right || e.KeyData == Keys.Divide)
        {
            day = 0;
            month = 0;
            year = 0;
        }
    }

    You can subscribe to this event in the CellEditorInitialized event handler like this:
    void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
            {
                if (e.Column is GridViewDateTimeColumn)
                {
                    RadDateTimeEditor editor = e.ActiveEditor as RadDateTimeEditor;
                    editor.CustomFormat = "yyyyMMdd";
     
                    RadDateTimeEditorElement element = editor.EditorElement as RadDateTimeEditorElement;
                    MaskDateTimeProvider provider = element.TextBoxElement.Provider as MaskDateTimeProvider;
                    provider.TextBoxItem.KeyUp -= TextBoxItem_KeyUp;
                    provider.TextBoxItem.KeyUp += TextBoxItem_KeyUp;             
                }
            }

    I have attached a sample demo project which demonstrates you the implementation. 

    I hope this helps. Should you have any other questions do not hesitate to ask.

    Regards,
    Ralitsa
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top