3
votes

Well the problem is that every time i edit a cell in my dataGridView and submit the change by pressing enter, it automatically moves the current cell selection to the next cell in the column. On the other hand, i want it to move to the next cell in the row, i've already written the code to do that, but i cant cancel the default action, and it changes both row and column.

Any help on how to stop the datagrid moving the selection to the next column cell on value change?

Thanks in advance.

1

1 Answers

2
votes

You can define a flag and raise it when an edit occurred (CellEndEdit event). Then in SelectionChanged event you check this flag and set a new position only if flag value is true.

In the code posted below for example I set current cell to the first cell of the DataGridView, avoiding default behavior:

private bool flag_cell_edited;

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    flag_cell_edited = true;
}

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (flag_cell_edited) {
        DataGridViewCell cell = dataGridView1.Rows[0].Cells[0];
        dataGridView1.CurrentCell = cell;
        //set flag value back to false
        flag_cell_edited = false;
    }
}