Enterキーのナビゲーションに関連する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);
したがって、Enterキーが押されたときの現在の動作は、デフォルトのままです。次の行と同じ列です。
何か案は?