4

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.

enter image description here

I don't want that, I want to exit edit mode but staying in the same cell.

enter image description here

Is it possible?

Escobar5
  • 3,941
  • 8
  • 39
  • 62

3 Answers3

5

You can customize the DataGridView to override the ProcessDialogKey method:

public class CustomGrid : DataGridView {            
        protected override bool ProcessDialogKey(Keys keyData) {
            if (keyData == Keys.Enter) {    
                EndEdit();
                return true;
            }
            return base.ProcessDialogKey(keyData);
        }
}
King King
  • 61,710
  • 16
  • 105
  • 130
1

KeyDown event:

If e.KeyCode = Keys.Enter Then e.SuppressKeyPress = True

Milton
  • 11
  • 1
0

Its very simple. While editing save the cellindex in a variable and then re-select it.

Get the column and row index:

e.ColumnIndex;
e.RowIndex;

and save them into a variable. After edited the cell, re-select the cell:

dataGridView1.Rows[rindex].Cells[cindex].Selected = true;
Shaharyar
  • 12,254
  • 4
  • 46
  • 66