【问题标题】:Disable scroll through DataGridViewComboBoxColumn and scroll through datagridview禁用通过 DataGridViewComboBoxColumn 滚动并通过 datagridview 滚动
【发布时间】:2013-03-06 14:00:03
【问题描述】:

我有一个包含两列(DataGridViewTextBoxColumnDataGRidViewComboBoxColumn)的 DataGridView。如果我单击文本框列中的单元格并使用鼠标滚轮滚动,则网格会滚动。太棒了。

如果我单击组合框列中的单元格,鼠标滚轮将滚动组合框中的项目。我需要滚动 datagridview。

在我尝试修复时,我可以通过处理 EditingControlShowing 事件来禁用组合框中的滚动:

private void SeismicDateGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
     if (e.Control is IDataGridViewEditingControl)
     {
          dgvCombo = (IDataGridViewEditingControl) e.Control;

          ((System.Windows.Forms.ComboBox)dgvCombo).MouseWheel -= new MouseEventHandler(DGVCombo_MouseWheel);
          ((System.Windows.Forms.ComboBox)dgvCombo).MouseWheel += new MouseEventHandler(DGVCombo_MouseWheel);
     }
}

private void DGVCombo_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
     HandledMouseEventArgs mwe = (HandledMouseEventArgs)e;
     mwe.Handled = true;
}

知道如何在DataGridViewComboBox 列处于活动状态时滚动 DataGridView 吗?

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    您是否考虑过处理 ComboBox 的 DropDownClosed 事件并将焦点更改为父级?

    void DateGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {            
        System.Windows.Forms.ComboBox comboBox = dataGridView.EditingControl as System.Windows.Forms.ComboBox;
        if (comboBox != null)
        {
            comboBox.DropDownClosed += comboBox_DropDownClosed;
        }
    }
    
    void comboBox_DropDownClosed(object sender, EventArgs e)
    {
        (sender as System.Windows.Forms.ComboBox).DropDownClosed -= comboBox_DropDownClosed;
        (sender as System.Windows.Forms.ComboBox).Parent.Focus();
    }
    

    如果您想在选择单元格之前滚动 DataGridView,但 ComboBox 仍然下拉,那将是另一种情况,但根据您在此处所说的判断:

    如果我单击组合框列中的一个单元格,鼠标滚轮将 滚动组合框中的项目。

    我假设您只是想在做出选择后更改焦点。

    【讨论】:

      【解决方案2】:

      您可以使用 P/Invoke 重定向输入,例如 here。或者您可以将DataGridView 子类化以向其添加Scroll 方法,该方法调用基类的OnMouseWheel 方法,然后您可以从DGVCombo_MouseWheel 调用该方法。示例here

      我认为第二个选项可能是最优雅的,没有理由使用 PInvoke。

      【讨论】:

        【解决方案3】:

        这里是通过内联函数完成的。并处理案例,当组合框被下拉时:

        dgv.EditingControlShowing += (s, e) =>
            {
                DataGridViewComboBoxEditingControl editingControl = e.Control as DataGridViewComboBoxEditingControl;
                if (editingControl != null)
                    editingControl.MouseWheel += (s2, e2) =>
                        {
                            if (!editingControl.DroppedDown)
                            {
                                ((HandledMouseEventArgs)e2).Handled = true;
                                dgv.Focus();
                            }
                        };
            };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多