2010-08-10 49 views
24

我感到惱火點擊一次在datagridview中選擇一行,然後再次單擊以點擊該行中的控件(在本例中爲組合框)。只需單擊一下,即可直接訪問DataGridView組合框?

有沒有辦法配置這個東西,所有這些都可以在一次鼠標點擊而不是兩次完成?

+0

您可能要檢查(http://stackoverflow.com/questions/34543940/datagridviewcomboboxcolumn-doesnt-open-the-dropdown-on-first [此解決方案。]點擊/ 39757746#39757746) – TaW 2016-09-28 21:18:43

回答

46

將DataGridView控件的EditMode屬性更改爲「EditOnEnter」。這將影響所有列。

+0

試試吧,謝謝。 – 2010-08-10 00:11:13

+0

正如我所希望的那樣工作。謝謝Stuart! – 2010-08-10 00:16:40

+0

在微軟的論壇上發佈了更好的解決方案。它將光標放在文本的中間,就像我想要的一樣:http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/fe5d5cfb-63b6-4a69-a01c-b7bbd18ae84a – HK1 2012-10-19 15:56:45

2

如果要選擇性地應用一鍵編輯某些列,可以MouseDown事件過程中切換當前單元格,以消除點擊編輯:

// Subscribe to DataGridView.MouseDown when convenient 
this.dataGridView.MouseDown += this.HandleDataGridViewMouseDown; 

private void HandleDataGridViewMouseDown(object sender, MouseEventArgs e) 
{ 
    // See where the click is occurring 
    DataGridView.HitTestInfo info = this.dataGridView.HitTest(e.X, e.Y); 

    if (info.Type == DataGridViewHitTestType.Cell) 
    { 
     switch (info.ColumnIndex) 
     { 
      // Add and remove case statements as necessary depending on 
      // which columns have ComboBoxes in them. 

      case 1: // Column index 1 
      case 2: // Column index 2 
       this.dataGridView.CurrentCell = 
        this.dataGridView.Rows[info.RowIndex].Cells[info.ColumnIndex]; 
       break; 
      default: 
       break; 
     } 
    } 
} 

當然,如果你的列和他們的索引是動態的,你需要稍微修改一下。

+0

Sooo很多datagridviews現在改變,如果我遇到一個情況下,我必須這樣做,我會檢查你的解決方案了! – 2010-08-10 00:25:12