如何在DataGridView中使用Enter键作为Tab键
问题描述:
我有一个5列的DataGridView
.如果在第一列中按Enter键,则焦点将移至下一行.我想在按Enter键时将焦点移到下一个列.
I have a DataGridView
with 5 columns. If press Enter key in the first column, focus moves to next row. I want to move the focus to the next column when I press Enter key.
private void dgvComp_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (dgvComp.CurrentRow.Cells[e.ColumnIndex].ReadOnly)
{
SendKeys.Send("{tab}");
}
}
在上面的代码中,我将第2、3和4列作为只读列.如果按Tab键,焦点应直接移至第5列.
In the above code I have columns 2,3 and 4 as read only columns. If I press Tab, focus should directly go to 5th column.
我该怎么做?
答
您能否尝试此解决方案
using System.Diagnostics;
class MyDataGridView : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter) {
base.ProcessTabKey(Keys.Tab);
return true;
}
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
base.ProcessTabKey(Keys.Tab);
return true;
}
return base.ProcessDataGridViewKey(e);
}
}