【问题标题】:How do I get DataGridView comboboxes to display their drop down list in one click?如何让 DataGridView 组合框一键显示其下拉列表?
【发布时间】:2011-05-16 19:08:31
【问题描述】:

将“EditOnEnter”设置为 true 后,如果我不单击组合框的向下箭头部分,DataGridViewComboBoxCell 仍需要单击两次才能打开。

有人知道如何解决这个问题吗?我有我自己的 DataGridView 类,我希望使用一些智能事件处理程序在系统范围内轻松解决此问题。

谢谢。

【问题讨论】:

    标签: c# .net winforms datagridview datagridviewcomboboxcell


    【解决方案1】:

    由于您已经将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 控件中的编辑控件获得输入焦点,上面的代码就会检查它是否是组合框。如果是这样,它virtually "presses" F4 键,这会导致下拉部分展开(当任何组合框有焦点时尝试它!)。这有点小技巧,但它就像一个魅力。

    【讨论】:

    • 我的那个 datagridview 类中已经有很多“黑客”了。再来一个就不会痛了。我今天会试试这个。
    • 我只是希望有一天这不会妨碍我使用 F 键在选项卡之间切换的计划。
    • @IsaacB:无论哪种方式,您都必须小心使用 F 键在选项卡之间切换。某些 F 键在 Windows 中具有您可能不应该覆盖的预定义用途。如上所示,每当任何组合框有焦点时,F4 都会使其下拉。 F10 总是激活应用程序的菜单系统。
    • 感谢您的警告。我对这一切都很绿。
    • 这并不是真正的完整解决方案,如果在编辑控件显示将在他们单击框以外的时间触发,例如,如果您单击选项卡控件中的另一个选项卡并单击返回到原始选项卡,或者当组合框是列表中的第一列时对 dgv 进行排序等。它只是打开了一个更大的蠕虫罐。
    【解决方案2】:

    为避免 SendKeys 问题,请尝试 Open dropdown(in a datagrid view) items on a single click 的解决方案。本质上,在组合框的 Enter 事件的 OnEditingControlShowing 挂钩中,在 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);
      }
      

      【讨论】:

        【解决方案4】:
            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;
                }
            }
        

        【讨论】:

        • 对你的回答做一点解释会很有帮助。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-08
        • 1970-01-01
        相关资源
        最近更新 更多