2017-01-10 5 views
0

Enterキーを押したときに次のセルをdatagridviewにフォーカスしたい。DataGridviewでTabの代わりにEnterを使用するWinform

だから私は、私はDataGridViewのクラスをオーバーライドするクラスを作成することができるに(here)が見つかりそうここにある:

using System.Diagnostics; 
using System.Windows.Forms; 

namespace PDP 

{ 
    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); 
     } 
    } 
} 

しかし、私は、「ENTER」キーを押すと、DataGridViewのは同じ動作とフォーカスを持っています現在のセルに留まる.. 私に何か不足していますか?

私は...、着信助けを

感謝をthisポストに解決を見たが、私は、キー入力を押したときにイベントが発生しません!イワンは私に尋ねたよう

は、私がこれをしなければならなかった、MyDatagridViewでのDataGridViewを置き換えるために逃した:

DataGridView datagridQa = new MyDatagridView(); 
+2

申し訳ありませんが、あなたはあなたのフォームの '' MyDataGridView'でDataGridView'を交換したことを確認していますか? –

+0

'ProcessDialogKey()'はまったく呼び出されていますか?もしそうであれば、 'base.ProcessTab()'ではなく 'base.ProcessDialogKey(Keys.Tab)'自身に転送することができます。私たちにとっては、 'ProcessDialogKey()'が呼ばれていますが、ここでフルキー処理を実装していますので、私はヒントがあなたを助けてくれるかどうかわかりません。 – Arvo

+2

愚かな質問@IvanStoevをありがとう、私はMyDataGridViewによってDataGridViewを置き換えていない....私の悪い –

答えて

0

を行ったEndEditイベントを保つためにCellEndEditイベントとブール変数で、すべての列と行のインデックスを取得します。

int columnIndex = 0; 
int rowIndex = 0; 
bool endEdit = false; 
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    columnIndex = dataGridView1.CurrentCell.ColumnIndex; 
    rowIndex = dataGridView1.CurrentCell.RowIndex; 
    endEdit = true; 
} 

次に、SelectionChangedイベントで新しいCurrentCellを設定します。 CellBeginEditEndEditイベントがSelectionChanged前にトリガーしたときにEnterを押す

private void dataGridView1_SelectionChanged(object sender, EventArgs e) 
     { 
      if (dataGridView1.CurrentCell != null && endEdit) 
      { 
       if (columnIndex == dataGridView1.Columns.Count - 1) 
        dataGridView1.CurrentCell = dataGridView1[0, rowIndex + 1]; 
       else 
        dataGridView1.CurrentCell = dataGridView1[columnIndex + 1, rowIndex]; 
       endEdit = false; 
      } 

     } 

は、デフォルトで次の行を焦点を当てています。

次のセルをナビゲートする場合でも、セル値を変更しないようにするため、Key_Downイベントを使用できます。ホープことができます

private void dataGridView1_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode == Keys.Enter) 
      { 
       e.SuppressKeyPress = true; 
       int columnIndex = dataGridView1.CurrentCell.ColumnIndex; 
       int rowIndex = dataGridView1.CurrentCell.RowIndex; 
       if (columnIndex == dataGridView1.Columns.Count - 1) 
        dataGridView1.CurrentCell = dataGridView1[0, rowIndex + 1]; 
       else 
        dataGridView1.CurrentCell = dataGridView1[columnIndex + 1, rowIndex]; 
      } 
      if (e.KeyCode == Keys.Tab) 
      { 
       e.Handled = true; 
      } 
     } 

、愚かな質問の

関連する問題