2010-08-10 55 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