1

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キーが押されたときの現在の動作は、デフォルトのままです。次の行と同じ列です。

何か案は?

4

2 に答える 2

0

KeyUpを試しましたか?別の行に移動するデフォルトの動作はKeyUpで発生し、テストを行い、Enterキーを押したままにして、列を下に移動するかどうか、またはキーアップを待機するかどうかを確認します。また、キーを押すこともできます。デフォルトのイベント動作の前にイベントが発生する可能性があるため、これらのそれぞれをテストして確認することもできます。

また、イベントサブスクリプションを持つコードが実行されていることを確認しますかそれが標準的な場所にある場合、私はそう思いますが、念のために実行されないメソッドにその行が配置されている可能性を捨てています。

于 2012-08-26T21:50:00.840 に答える
0
 private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
 {
    if (e.KeyCode == Keys.Enter)
    {
        e.SuppressKeyPress = true;
        e.Handled = true;
        SendKeys.Send("{tab}");
    }
 }
于 2013-03-09T18:06:25.410 に答える