2010-11-28 90 views
5

将“EditOnEnter”设置为true后,如果我没有单击组合框的向下箭头部分,DataGridViewComboBoxCell仍然需要两次点击才能打开。如何让DataGridView组合框在单击时显示其下拉列表?

任何人都有任何线索如何解决这个问题?我有我自己使用的DataGridView类,所以我可以通过一些我希望的智能事件处理程序轻松地解决系统范围内的这个问题。

谢谢。

回答

5

既然你已经有DataGridViewEditMode属性设置为‘EditOnEnter’,你可以重写其OnEditingControlShowing方法,以确保作为一个组合框获得焦点尽快所示的下拉列表:

public class myDataGridView : DataGridView 
{ 

    protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e) 
    { 
     base.OnEditingControlShowing(e); 

     if (e.Control is ComboBox) { 
      SendKeys.Send("{F4}"); 
     } 
    } 

} 

只要您的DataGridView控件中的编辑控件获得输入焦点,上面的代码就会检查它是否是组合框。如果是这样,那么会导致下拉部分展开的F4键(当任何组合框具有焦点时尝试它)。这有点破解,但它像一个魅力。

+0

我已经得到了很多在datagridview的类矿山的“黑客”的。还有一件事不会受到伤害。我今天会试试这个。 – 2010-11-28 17:07:03

+0

以上是我的猫! – 2010-11-28 17:11:26

+0

的确很有魅力! – 2010-11-28 18:29:00

0

要避免的SendKeys问题,从Open dropdown(in a datagrid view) items on a single click尝试的解决方案。实质上,在OnEditingControlShowing钩子组合框的Enter事件中,在Enter事件处理程序中,设置ComboBox.DroppedDown = true。这似乎有同样的效果,但没有副作用@Cody Gray提到。

3

我用这个解决方案,因为它避免了发送键击:

覆盖的OnCellClick方法(如果你继承)或订阅CellClick事件(如果你从另一个对象,而不是作为一个改变DGV子类)。

protected override void OnCellClick(DataGridViewCellEventArgs e) 
{ 
    // Normally the user would need to click a combo box cell once to 
    // activate it and then again to drop the list down--this is annoying for 
    // our purposes so let the user activate the drop-down with a single click. 
    if (e.ColumnIndex == this.Columns["YourDropDownColumnName"].Index 
     && e.RowIndex >= 0 
     && e.RowIndex <= this.Rows.Count) 
    { 
     this.CurrentCell = this[e.ColumnIndex, e.RowIndex]; 
     this.BeginEdit(false); 
     ComboBox comboBox = this.EditingControl as ComboBox; 
     if (comboBox != null) 
     { 
      comboBox.DroppedDown = true; 
     } 
    } 

    base.OnCellContentClick(e); 
} 
1
protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e) 
    { 
     base.OnEditingControlShowing(e); 
     DataGridViewComboBoxEditingControl dataGridViewComboBoxEditingControl = e.Control as DataGridViewComboBoxEditingControl; 
     if (dataGridViewComboBoxEditingControl != null) 
     { 
      dataGridViewComboBoxEditingControl.GotFocus += this.DataGridViewComboBoxEditingControl_GotFocus; 
      dataGridViewComboBoxEditingControl.Disposed += this.DataGridViewComboBoxEditingControl_Disposed; 
     } 
    } 

    private void DataGridViewComboBoxEditingControl_GotFocus(object sender, EventArgs e) 
    { 
     ComboBox comboBox = sender as ComboBox; 
     if (comboBox != null) 
     { 
      if (!comboBox.DroppedDown) 
      { 
       comboBox.DroppedDown = true; 
      } 
     } 
    } 

    private void DataGridViewComboBoxEditingControl_Disposed(object sender, EventArgs e) 
    { 
     Control control = sender as Control; 
     if (control != null) 
     { 
      control.GotFocus -= this.DataGridViewComboBoxEditingControl_GotFocus; 
      control.Disposed -= this.DataGridViewComboBoxEditingControl_Disposed; 
     } 
    }