Enter Key Behaviour when Editing Cells

15 posts, 1 answers
  1. Mark
    Mark avatar
    36 posts
    Member since:
    Jan 2012

    Posted 10 Sep 2012 Link to this post

    Hi,

    I have a Grid with a number of read only columns and two editable columns and would like the ENTER key and TAB key to behave the same when editing data..

    When I am editing a value in a cell and press TAB, focus skips over any read only columns in the row and moves to the next editable column. When I TAB off that, focus moves to the first editable column of the next row. This is desired behaviour.

    When I am editing a cell and press ENTER though, focus moves to the next column whether it is read only or not. I have set EnterKeyMode to NextCell.

    Is there any way to simulate the tab behaviour when the Enter Key is pressed?

    Thanks,

    Mark.
  2. Answer
    Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 10 Sep 2012 Link to this post

    Hello Mark, 

    Thank you for writing.

    To achieve the desired functionality, you should create a custom grid behavior in the following way:
    class MyBehavior : BaseGridBehavior
    {
        public override bool ProcessKeyDown(KeyEventArgs keys)
        {
            if (keys.KeyData == Keys.Enter && this.GridControl.IsInEditMode)
            {
                this.GridControl.GridNavigator.SelectNextColumn();
            }
            return true;
        }
    }

    Then, simply assign this custom behavior to your grid:
    radGridView1.GridBehavior = new MyBehavior();

    I hope this helps.

    Greetings,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  3. Mark
    Mark avatar
    36 posts
    Member since:
    Jan 2012

    Posted 11 Sep 2012 Link to this post



    Great stuff! Works a treat.

    Many thanks,

    Mark.
  4. Rawad
    Rawad avatar
    23 posts
    Member since:
    Jan 2012

    Posted 07 Jul 2014 Link to this post

    Dear 

    I tried the following code :

      Private Sub RadGridView1_PreviewKeyDown(sender As System.Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles RadGridView1.PreviewKeyDown
            If e.KeyData = Keys.Enter Then
                Me.RadGridView1.GridNavigator.SelectNextRow(1)
            End If
        End Sub

    but after editing the cell, I need to Press Enter twice (2 times) in order to move to the second row.
    there's a solution ?
    And lets say I have I have 3 columns, and just second column is editable,  how to make it by default cell selective, in another term, wherever user click in any cell, or press enter in any cell,   just cell of column 2 be selected.

    Thanks
  5. Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 10 Jul 2014 Link to this post

    Hi Rawad,

    Thank you for writing.

    You have to press enter key twice because the first time you do it, the grid will go out of edit mode and the next one will move the selection. The solution provided with the previous post is suitable for both of your requirements. Feel free to use it.

    If need be, you can use our free online C# to VB converter: http://converter.telerik.com/.

    I hope that you find this information useful.

    Regards,
    Stefan
    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.
     
  6. Rawad
    Rawad avatar
    23 posts
    Member since:
    Jan 2012

    Posted 10 Jul 2014 Link to this post

    yes dear , thanks a lot,  it's working well.

    and another solution can be:

     Private Sub RadGridView1_PreviewKeyDown(sender As System.Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles RadGridView1.PreviewKeyDown
            If e.KeyData = Keys.Enter Then
                If RadGridView1.CurrentRow.Index + 1 = RadGridView1.Rows.Count Then
                    Me.RadGridView1.GridNavigator.SelectFirstRow()
                    Me.RadGridView1.GridNavigator.SelectFirstColumn()
                Else
                    Me.RadGridView1.GridNavigator.SelectNextRow(1)
                    Me.RadGridView1.GridNavigator.SelectFirstColumn()
                End If
            End If
        End Sub

        Private Sub RadGridView1_CellEndEdit(sender As System.Object, e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles RadGridView1.CellEndEdit
            If RadGridView1.CurrentRow.Index + 1 = RadGridView1.Rows.Count Then
                Me.RadGridView1.GridNavigator.SelectFirstRow()
                Me.RadGridView1.GridNavigator.SelectFirstColumn()
            Else
                Me.RadGridView1.GridNavigator.SelectNextRow(1)
                Me.RadGridView1.GridNavigator.SelectFirstColumn()
            End If
        End Sub
  7. Abdul
    Abdul avatar
    27 posts
    Member since:
    Jan 2017

    Posted 28 Jun 2017 Link to this post

    Hi Stefan,

    I want to customize RadGridView cell focus(Tab Key) on Enter Key based on RadGridView.Column index(I developed a Settings for RadGridView in which customer can change column index,readonly,visible,etc).So RadGridView.Column index is not fixed,it will change.

    Hope you got my question.If you need more clarification,please let me know.

    Regards

    ABDUL HAFEEL

  8. Dimitar
    Admin
    Dimitar avatar
    2736 posts

    Posted 28 Jun 2017 Link to this post

    Hi Abdul,

    This can be achieved by setting the EnterKeyMode property:
    radGridView1.EnterKeyMode = RadGridViewEnterKeyMode.EnterMovesToNextCell;

    I hope this will be useful. 

    Regards,
    Dimitar
    Progress Telerik
    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.
  9. Abdul
    Abdul avatar
    27 posts
    Member since:
    Jan 2017

    Posted 29 Jun 2017 in reply to Dimitar Link to this post

    Hi Dimitar,

    Thank you for your quick reply

    In my case,I want to implement this,when pressing an Enter Key moves to a particular cell not the next cell.

    How can i set TabIndex for each cell in RadGridView?

    Regards

    ABDUL HAFEEL

  10. Dimitar
    Admin
    Dimitar avatar
    2736 posts

    Posted 29 Jun 2017 Link to this post

    Hi Abdul,

    This can be achieved by using custom row behavior which allows you to handle the enter key. You can use the Tag property to store the tab indexes. Here is a sample implementation:
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            radGridView1.DataSource = GetTable();
     
            BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
            gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
            gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridDataRowBehavior());
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            int count = 100;
            foreach (var row in radGridView1.Rows)
            {
                foreach (GridViewCellInfo cell in row.Cells)
                {
                    cell.Tag = count--;
                }
            }
        }
        static DataTable GetTable()
        {
     
            DataTable table = new DataTable();
            table.Columns.Add("Dosage", typeof(int));
            table.Columns.Add("Drug", typeof(string));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));
     
            table.Rows.Add(25, "Indocin", "David", DateTime.Now);
            table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
            table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
            table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
            table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
            return table;
        }
     
    }
    public class CustomGridDataRowBehavior : GridDataRowBehavior
    {
        protected override bool ProcessEnterKey(KeyEventArgs keys)
        {
            int currentIndex = (int)this.GridControl.CurrentRow.Cells[this.GridControl.CurrentColumn.Name].Tag;
            
            foreach (var row in GridControl.Rows)
            {
                foreach (GridViewCellInfo cell in row.Cells)
                {
                    if ((int)cell.Tag == currentIndex + 1)
                    {
                        this.GridControl.CurrentRow = row;
                        this.GridControl.CurrentColumn = cell.ColumnInfo;
                        break;
                    }
                }
            }
            return true;
        }
    }

    I hope this will be useful.

    Regards,
    Dimitar
    Progress Telerik
    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.
  11. Bao
    Bao avatar
    54 posts
    Member since:
    Jan 2015

    Posted 13 Sep 2017 in reply to Stefan Link to this post

    Hi Stefan,

    Is there any reason ProcessKeyDown can not be called ? Because i do same steps but it can not call to custom behavior .

  12. Dimitar
    Admin
    Dimitar avatar
    2736 posts

    Posted 14 Sep 2017 Link to this post

    Hello Bao,

    I am not sure why this is not working on your side. I would suggest you open a support ticket and attach your project. This will allow us to properly investigate this case and provide you with a solution.

    Should you have any other questions do not hesitate to ask.

    Regards,
    Dimitar
    Progress Telerik
    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.
  13. Susan
    Susan avatar
    8 posts
    Member since:
    Jun 2014

    Posted 20 Jan 2018 Link to this post

    I am trying to solve the exact thing the original poster posted about.  I have tried the solution that was given to him (the ProcessKeyDown) method.  It does go to the next editable cell however when the last cell is edited and the enter key is pressed, it just stays on that row. I need it to go to the first editable cell on the next row (in my current case a new row).

    I just updated to 2018 q 1 this morning.

  14. Dimitar
    Admin
    Dimitar avatar
    2736 posts

    Posted 22 Jan 2018 Link to this post

    Hi Susan,

    I have tested this and it appears to work on my side. I have attached my test project. Could you please check it and let me know what is different on your side?

    I am looking forward to your reply.

    Regards,
    Dimitar
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  15. Yeepy
    Yeepy avatar
    1 posts
    Member since:
    Jul 2019

    Posted 27 Jul in reply to Rawad Link to this post

    Thanks a lot. this help me too
Back to Top