DataGridView / Enter Key

datagridview

I'm using vb.net 2008 and DataGridView. I'm looking for code that will allow me to have the enter key move to the next column to the right instead of moving down one row while remaining in the same column.

Best Answer

If you are confirming an edit, just move to the next column in the event CellEndEdit. If you are not in edit mode, you need to override ProcessDialogKey. See example below:

public class dgv : DataGridView
{
    protected override bool ProcessDialogKey(Keys keyData)
    {
        Keys key = (keyData & Keys.KeyCode);
        if (key == Keys.Enter)
        {
            return this.ProcessRightKey(keyData);
        }
        return base.ProcessDialogKey(keyData);
    }
    protected override bool ProcessDataGridViewKey(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            return this.ProcessRightKey(e.KeyData);
        }
        return base.ProcessDataGridViewKey(e);
    }
}