Click here to Skip to main content
14,700,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Moves the focus to the next cell in the current row. If the focus is in the last cell of the row, moves the focus to the first cell in the next row. If the focus is in the last cell in the control, moves the focus to the next control in the tab order of the parent container.
If the current cell is in edit mode and pressing TAB causes focus to move away from the current row, any changes that were made to the row are committed before focus is changed.
but i want to make these all tab functions on Enter Key
Can any one help me...


What I have tried:

i Am using this code
private void Earnings_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Escape)
{
this.Close();
}
else if(e.KeyCode==Keys.Enter)
{
SendKeys.Send("{TAB}");
e.Handled = true;
e.SuppressKeyPress = true;
}
else if(e.KeyCode==Keys.Enter)
{
SendKeys.Send("{home}");//go to first column
resetRow = true;
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.ColumnCount - 1 == e.ColumnIndex) //if last column
{
KeyEventArgs forKeyDown = new KeyEventArgs(Keys.Enter);
resetRow = false;
Earnings_KeyDown(dataGridView1, forKeyDown);
}
else
{
SendKeys.Send("{up}");
SendKeys.Send("{right}");
}
}
Posted
Updated 6-Aug-16 22:40pm
v3

1 solution

It is nearly impossible to solve this request by using only the "standard events". Especially handling of ENTER while the grid is in edit mode I don't see how to solve this.

Therefore I suggest to solve the request by introducing your own grid derived from DataGridView. On a first glance it would maybe sound difficult, but please don't be afraid, it is easier than you think.

Also an advantage of this approach is, you can use your "new" grid in any form/app without the need of reimplement all this key up/down etc. handlers. Finally you will learn some Basics/get a feeling how to make your own Controls.

To make it easier at the beginning I recommend to add that user control first in a small test project to test and adapt it for your requirements. After you are happy with the control you can later move it to a seperate assembly and use it in any of your project.

Most Information for the Special ENTER handling I found here: https://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=DE-DE&k=k(System.Windows.Forms.DataGridView.ProcessDialogKey);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd=true[^]
public class DataGridViewX : DataGridView
{
    // A public property which controls the behaviour. You can set this
    // programatically or you will find it also in the VS property view.
    //      true    : Default behaviour, like DataGridView
    //      false   : Special ENTER key handling
    public bool DefaultEnterKeyBehavior { get; set; }

    public DataGridViewX()
    {
        DefaultEnterKeyBehavior = true;
        //InitializeComponent();
    }


    //
    // https://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=DE-DE&k=k(System.Windows.Forms.DataGridView.ProcessDialogKey);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd=true
    //
    [System.Security.Permissions.UIPermission(
        System.Security.Permissions.SecurityAction.LinkDemand,
        Window = System.Security.Permissions.UIPermissionWindow.AllWindows)]
    protected override bool ProcessDialogKey(Keys keyData)
    {
        // This method will handle ENTER while grid is InEdit

        // Extract the key code from the key value. 
        Keys key = (keyData & Keys.KeyCode);

        // Default behaviour are any other key than ENTER
        if (DefaultEnterKeyBehavior || (key != Keys.Enter))
        {
            return base.ProcessDialogKey(keyData);
        }

        // Special handling for ENTER key
        return false;
    }

    //
    // https://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=DE-DE&k=k(System.Windows.Forms.DataGridView.ProcessDialogKey);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd=true
    //
    [System.Security.Permissions.SecurityPermission(
        System.Security.Permissions.SecurityAction.LinkDemand, Flags =
        System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
    protected override bool ProcessDataGridViewKey(KeyEventArgs e)
    {
        // Default behaviour are any other key than ENTER 
        if (DefaultEnterKeyBehavior || (e.KeyCode != Keys.Enter))
        {
            return base.ProcessDataGridViewKey(e);
        }
        else
        {
            // Special ENTER key handling
            if (this.CurrentCell.ColumnIndex < (this.ColumnCount - 1))
            {
                // Next Column
                this.ProcessRightKey(e.KeyData);
            }
            else if (this.CurrentCell.RowIndex < (this.RowCount - 1))
            {
                // Next Row, Column 0
                this.ProcessHomeKey(e.KeyData);
                this.ProcessDownKey(e.KeyData);
            }
            else
            {
                // Simulate Tab, not nice... but did not find a better solution until now for this 
                SendKeys.Send("{TAB}");
            }
            // Either way we handled the ENTER key
            return true;
        }
    }
}

A remark to the multiply return in the methods:
Usually I avoid to have multiply return statements in a method. But in this case -whyever- for me the code is easier to read with them...

Final remarks:
- When using the new grid dont't Forget to set the property DefaultEnterKeyBehavior to false, either in designer or in code.
- For background information about creating user controls for .NET you will find a lot of tutorials by google.
- Try to find a better name than DataGridViewX :)
I hope it helps.
   
v3
Comments
Member 12365816 11-Aug-16 1:33am
   
i like your suggestion and i use it your recommendation .its working good but its not meet my other requirements.
Member 12365816 11-Aug-16 1:36am
   
i am also using the other event like specific cell leave,selected value changed key down except Enter Key,Datagridview design and painting event and much more...
0x01AA 11-Aug-16 1:46am
   
I see, sorry I concentrated only on ENTER move.
So for i.e. EndEdit you can override protected virtual void OnCellEndEdit(DataGridViewCellEventArgs e); of DataGridView.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900