2009-04-10 25 views
2

私のクライアントは、DataGridViewセルを介してタブするときに、現在の次のセルがデフォルト以外のセルになるようにしたいと考えています。これを達成する最良の方法は何ですか?DataGridViewでタブの順序を変更するにはどうすればよいですか?

+0

独自のグリッドを書きたくない場合は、KeyUp/KeyDownイベントを使用します。私の答えを見てください。私はそれを試して、それは動作するようだ... – Ruslan

答えて

2

独自のDataGridViewを作成し、ProcessTabKeyメソッドをオーバーライドします。そこにロジックがありますか?SetCurrentCellAddressCoreを使用して、次のアクティブなセルを設定します。

など、そのメソッドのデフォルトの実装では、このような選択モード、編集モード、行の状態など、さまざまな条件を占めていることを注意、境界

編集

また、あなたは/ keyUpイベントを処理することができKeyDownイベント。 Trueに、グリッドの

設定StandardTabプロパティを、次のコードを追加します:、そこでのいくつかの奇妙な行動があると私は多くの時間を費やすことはありませんでしたが、これはやるべき

private void Form1_Load(object sender, EventArgs e) 
{ 
    // TODO: Load Data 

    dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0]; 

    if (dataGridView1.CurrentCell.ReadOnly) 
     dataGridView1.CurrentCell = GetNextCell(dataGridView1.CurrentCell); 
} 

private void dataGridView1_KeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Tab) 
    { 
     dataGridView1.CurrentCell = GetNextCell(dataGridView1.CurrentCell); 
     e.Handled = true; 
    } 
} 

private DataGridViewCell GetNextCell(DataGridViewCell currentCell) 
{ 
    int i = 0; 
    DataGridViewCell nextCell = currentCell; 

    do 
    { 
     int nextCellIndex = (nextCell.ColumnIndex + 1) % dataGridView1.ColumnCount; 
     int nextRowIndex = nextCellIndex == 0 ? (nextCell.RowIndex + 1) % dataGridView1.RowCount : nextCell.RowIndex; 
     nextCell = dataGridView1.Rows[nextRowIndex].Cells[nextCellIndex]; 
     i++; 
    } while (i < dataGridView1.RowCount * dataGridView1.ColumnCount && nextCell.ReadOnly); 

    return nextCell; 
} 
0

氏ルスランをコードは私のために働かなかった。彼の考えを引き出すことで、私はコードを少し変えました。

private void dataGridView1_KeyUp(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Tab) 
     { 
      if(dataGridView1.CurrentCell.ReadOnly) 
       dataGridView1.CurrentCell = GetNextCell(dataGridView1.CurrentCell); 
      e.Handled = true; 
     } 
    } 

    private DataGridViewCell GetNextCell(DataGridViewCell currentCell) 
    { 
     int i = 0; 
     DataGridViewCell nextCell = currentCell; 

     do 
     { 
      int nextCellIndex = (nextCell.ColumnIndex + 1) % dataGridView1.ColumnCount; 
      int nextRowIndex = nextCellIndex == 0 ? (nextCell.RowIndex + 1) % dataGridView1.RowCount : nextCell.RowIndex; 
      nextCell = dataGridView1.Rows[nextRowIndex].Cells[nextCellIndex]; 
      i++; 
     } while (i < dataGridView1.RowCount * dataGridView1.ColumnCount && nextCell.ReadOnly); 

     return nextCell; 
    } 
関連する問題