2013-07-22 33 views
1

我只想在特定列上設置製表順序。如我有2列(ID和名稱)。所以選項卡僅反映在「名稱」列上。當我按Tab鍵時,它會垂直移動到同一列中的下一行。如何僅在特定列datagridview中設置製表順序

+0

我不明白請編輯,顯示一些代碼或解釋更多 – 2013-07-22 16:48:10

+1

您使用的是什麼樣的控件? @King King可能在正確的軌道上,但我會使用控件KeyDown Event,如果您使用的是GridView,那麼您應該能夠將其ID列的TabStop設置爲false。 – Bit

+0

@ N4TKD你說得對,至少可以在'KeyDown'中處理'Tab',也許我們無法在'KeyDown'中處理的唯一密鑰就是'Enter',那就是你必須使用ProcessDataGridViewKey。 –

回答

2

我認爲你必須重寫ProcessDataGridViewKey方法趕上Tab鍵並選擇單元格自己,就像這樣:

public class CustomDataGridView : DataGridView 
{  
    //This contains all the column indices in which the tab will be switched vertically. For your case, the initial index is 1 (the second column): VerticalTabColumnIndices.Add(1); 
    public List<int> VerticalTabColumnIndices = new List<int>();  
    protected override bool ProcessDataGridViewKey(KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Tab&&VerticalTabColumnIndices.Contains(CurrentCell.ColumnIndex)) 
     { 
      int i = (CurrentCell.RowIndex + 1) % Rows.Count; 
      CurrentCell = Rows[i].Cells[CurrentCell.ColumnIndex]; 
      return true;//Suppress the default behaviour which switches to the next cell. 
     } 
     return base.ProcessDataGridViewKey(e); 
    } 
} 
//or simply you can handle the Tab key in a KeyDown event handler 
private void KeyDownHandler(object sender, KeyEventArgs e){ 
    if(e.KeyCode == Keys.Tab){ 
    e.Handled = true; 
    //remaining code... 
    //..... 
    } 
} 
//in case you are not familiar with event subscribing 
yourDataGridView.KeyDown += KeyDownHandler; 
+0

從上面回答我面臨同樣的問題。 –

+0

@ user2607573該代碼運行良好,因爲我測試過它,如果不適合你,你應該考慮一切,如果可能的話。代碼與向前選項卡,而不是向後選項卡(SHIT + TAB)一起使用。 –

0

列在dataridview沒有tab順序
1),則可以覆蓋處理選項卡的keydown事件。
2)另一個簡單的方法是按下向上,向下,向左,向右鍵導航數據網格視圖。

相關問題