On Enter key move to next row and skip group header focus

7 posts, 0 answers
  1. Tomas
    Tomas avatar
    4 posts
    Member since:
    Apr 2017

    Posted 23 Jul Link to this post

    Good afternoon,

    I would like to change grid cursor movement behavior when pressing Enter key in the grid with grouped records. I need to move cursor only through the column cells without focusing a group header.  Is it doable?

    My grid settings are:

    radGridView1.MasterTemplate.AutoExpandGroups = true;
    radGridView1.EnterKeyMode = RadGridViewEnterKeyMode.EnterMovesToNextRow;
  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3895 posts

    Posted 23 Jul Link to this post

    Hello, Tomas,

    RadGridView manages user mouse and keyboard input over its rows by GridRowBehavior. Depending on the row type, RadGridView introduces different behaviors: https://docs.telerik.com/devtools/winforms/controls/gridview/rows/row-behaviors 

    Thus, you can create a derivative of the respective row behavior that is needed in your scenario and override the respective method that handles the Enter key and execute the desired action, e.g. override the ProcessEnterKey method.

    I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik

  3. Tomas
    Tomas avatar
    4 posts
    Member since:
    Apr 2017

    Posted 24 Jul in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Hello, Dess,

    many thanks for the advice. I was successfull. The solution is not very nice, but it works as expected. If you see any issue with this solution, please notify me.

    I registered my CustomGridRowBehavior() right after grid initialization:

    BaseGridBehavior gridBehavior = rgvResults.GridBehavior as BaseGridBehavior;
    gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
    gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridRowBehavior());

     

    Then I prepared the implementation of derived class:

    class CustomGridRowBehavior : GridDataRowBehavior
    {
        protected override bool ProcessEnterKey(KeyEventArgs keys)
        {
            this.GridControl.GridNavigator.SelectNextRow(1);
            if (this.GridControl.CurrentRow.RowElementType.Name == "GridGroupHeaderRowElement")
            {
                this.GridControl.GridNavigator.SelectNextRow(1);
            }
            return base.ProcessF2Key(keys);
        }
    }
  4. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3895 posts

    Posted 24 Jul Link to this post

    Hi, Tomas,

    I am glad that you have implemented an appropriate row behavior for your scenario. I have reviewed the provided code snippet and it seems good to me.

    Just a small suggestion, instead of checking the name of the RowElementType, it is OK to check the type of the CurrentRow itself.

    Should you have further questions please let me know.

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik

  5. Tomas
    Tomas avatar
    4 posts
    Member since:
    Apr 2017

    Posted 24 Jul in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Hi, Dess,

    to be honest I did not figure out how to get type of CurrentRow and do the comparison to check if it is GridGroupHeaderRowElement. If you have spare time, could you post a code snippet how to beat this? It is not important, just for study purposes.

    Have a great day,

    Tomas

  6. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3895 posts

    Posted 27 Jul Link to this post

    Hello, Tomas, 

    Firstly, I would like to note that for GridViewGroupRowInfos, RadGridView offers a specific row behavior for handling the mouse/keyboard input: GridGroupRowBehavior. If you want to handle pressing Enter key when the group row is current, it is more appropriate to use it:
            public RadForm1()
            {
                InitializeComponent();
    
                BaseGridBehavior gridBehavior = this.radGridView1.GridBehavior as BaseGridBehavior;
                gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
                gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridRowBehavior());
    
                gridBehavior.UnregisterBehavior(typeof(GridViewGroupRowInfo));
                gridBehavior.RegisterBehavior(typeof(GridViewGroupRowInfo), new CustomGridGroupRowBehavior());
            }
    
            public class CustomGridGroupRowBehavior : GridGroupRowBehavior
            {
                protected override bool ProcessEnterKey(KeyEventArgs keys)
                {
    
                    return base.ProcessEnterKey(keys);
                }
            }

    You can plug into the above code and execute the desired logic.

    As to the question about checking the CurrentRow's type, please have a look at this code snippet:

                    if (this.GridControl.CurrentRow is GridViewGroupRowInfo)
                    {
    
                    }

    Should you have further questions please let me know.

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik

  7. Tomas
    Tomas avatar
    4 posts
    Member since:
    Apr 2017

    Posted 03 Aug in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Hi Dess, that is brilliant. Many thanks.
Back to Top