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

Enter Key Behaviour when Editing Cells

14 Answers 1410 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 10 Sep 2012, 12:23 PM
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.

14 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 10 Sep 2012, 01:09 PM
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 >>
0
Mark
Top achievements
Rank 1
answered on 11 Sep 2012, 02:37 PM


Great stuff! Works a treat.

Many thanks,

Mark.
0
Rawad
Top achievements
Rank 2
answered on 07 Jul 2014, 08:29 AM
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
0
Stefan
Telerik team
answered on 10 Jul 2014, 06:06 AM
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.
 
0
Rawad
Top achievements
Rank 2
answered on 10 Jul 2014, 06:59 AM
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
0
Abdul
Top achievements
Rank 1
answered on 28 Jun 2017, 06:43 AM

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

0
Dimitar
Telerik team
answered on 28 Jun 2017, 11:28 AM
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.
0
Abdul
Top achievements
Rank 1
answered on 29 Jun 2017, 07:03 AM

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

0
Dimitar
Telerik team
answered on 29 Jun 2017, 08:31 AM
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.
0
Bao
Top achievements
Rank 1
answered on 13 Sep 2017, 11:51 PM

Hi Stefan,

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

0
Dimitar
Telerik team
answered on 14 Sep 2017, 08:12 AM
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.
0
Susan
Top achievements
Rank 1
answered on 20 Jan 2018, 09:22 PM

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.

0
Dimitar
Telerik team
answered on 22 Jan 2018, 09:23 AM
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.
0
Yeepy
Top achievements
Rank 1
answered on 28 Jul 2019, 01:47 AM
Thanks a lot. this help me too
Tags
GridView
Asked by
Mark
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Mark
Top achievements
Rank 1
Rawad
Top achievements
Rank 2
Abdul
Top achievements
Rank 1
Dimitar
Telerik team
Bao
Top achievements
Rank 1
Susan
Top achievements
Rank 1
Yeepy
Top achievements
Rank 1
Share this question
or