Prevent datagridview form windows for changing row after cell editing

I have a datagridview on Windows forms, the default behavior when editing the values ​​is that after the cell is released, when I press the enter button, the selected row changes to the next.

enter image description here

I do not want this, I want to exit the editing mode, but remain in the same cell.

enter image description here

Is it possible?

+4
source share
3 answers

You can configure 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); } } 
+5
source

KeyDown Event:

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

+1
source

It is very simple. During editing, save cellindex in a variable, and then reselect it.

Get the column and row index:

 e.ColumnIndex; e.RowIndex; 

and save them in a variable. After editing the cell, select the cell again:

 dataGridView1.Rows[rindex].Cells[cindex].Selected = true; 
0
source

Source: https://habr.com/ru/post/1499447/


All Articles