C# 防止windows窗体datagridview在单元格编辑后更改行
我在windows窗体中有一个datagridview,编辑值时的默认行为是,编辑单元格后,当我按enter键时,所选行将更改为下一行 我不想这样,我想退出编辑模式,但留在同一个单元格中C# 防止windows窗体datagridview在单元格编辑后更改行,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,我在windows窗体中有一个datagridview,编辑值时的默认行为是,编辑单元格后,当我按enter键时,所选行将更改为下一行 我不想这样,我想退出编辑模式,但留在同一个单元格中 可能吗?很简单。编辑时,将cellindex保存在变量中,然后重新选择它 获取列和行索引: e.ColumnIndex; e.RowIndex; 并将它们保存到变量中。编辑单元后,重新选择单元: dataGridView1.Rows[rindex].Cells[cindex].Selected = tru
可能吗?很简单。编辑时,将
cellindex
保存在变量中,然后重新选择它
获取列和行索引:
e.ColumnIndex;
e.RowIndex;
并将它们保存到变量中。编辑单元后,重新选择单元:
dataGridView1.Rows[rindex].Cells[cindex].Selected = true;
您可以自定义
DataGridView
以覆盖ProcessDialogKey
方法:
public class CustomGrid : DataGridView {
protected override bool ProcessDialogKey(Keys keyData) {
if (keyData == Keys.Enter) {
EndEdit();
return true;
}
return base.ProcessDialogKey(keyData);
}
}
按键关闭事件:
如果e.KeyCode=Keys.Enter,则e.SuppressKeyPress=True我已搜索但未找到它。谢谢