我想修改与输入键导航相关的 datagridview 的当前行为。当前的行为是跳到下一行和同一列,我想跳到下一列和同一行,所以我实现了以下 keyDown 事件:
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int numCols = this.dataGridView1.ColumnCount;
int numRows = this.dataGridView1.RowCount;
DataGridViewCell currCell = this.dataGridView1.CurrentCell;
if (currCell.ColumnIndex == numCols - 1)
{
if (currCell.RowIndex < numRows - 1)
{
this.dataGridView1.CurrentCell = this.dataGridView1[0, currCell.RowIndex + 1];
}
}
else
{
this.dataGridView1.CurrentCell = this.dataGridView1[currCell.ColumnIndex + 1, currCell.RowIndex];
}
e.Handled = true;
}
}
问题是,尽管我已通过以下操作正确订阅了 datagridview keydown 事件,但在按下 enter 键时并未引发上述事件:
this.dataGridView1.KeyDown += new KeyEventHandler(dataGridView1_KeyDown);
因此,按下回车键时的当前行为仍然是默认行为:下一行和同一列。
有任何想法吗?