Prevent windows forms datagridview to change column after cell edit

25 Views Asked by At

I have a datagridview in windows forms, the default behavior when editing values is that after the edition of a cell, when I press enter, the selected row changes to the next

my question is that change column after cell edit and not change row to next

1

There are 1 best solutions below

0
On

You should handle the dataGridView1_KeyDown event.

I didn't really tested the code, but I think it's something like this.

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData == Keys.Enter)
    {
        int col = dataGridView1.CurrentCell.ColumnIndex;
        int row = dataGridView1.CurrentCell.RowIndex;

        dataGridView1.CurrentCell = dataGridView1[col, row];
        e.SuppressKeyPress = true;
        e.Handled = true;
    }
}