2013-03-06 114 views
4

我有一个DataGridView有两列(DataGridViewTextBoxColumnDataGRidViewComboBoxColumn)。如果我点击文本框列中的一个单元格并用鼠标滚轮滚动,则网格滚动。太棒了。禁用滚动通过DataGridViewComboBoxColumn和滚动通过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; 
} 

任何想法如何滚动DataGridView的时候DataGridViewComboBox列是活动的?

回答

2

您是否考虑过处理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:

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

我假设你只是想改变聚焦一旦已经做出选择。

+0

这使得它可以按照我的需要工作。谢谢。 – Web 2013-03-25 17:17:58

1

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

我觉得第二个选择可能是最优雅的,没有理由使用PInvoke。

1

这里是用内联函数完成的。处理情况下,当组合框被丢弃时:

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(); 
        } 
       }; 
    };