c# – 禁止滚动DataGridViewComboBoxColumn并滚动数据网格视图

我有一个DataGridView有两列(DataGridViewTextBoxColumn和DataGRidViewComboBoxColumn).如果单击文本框列中的单元格并使用鼠标滚动滚动,则网格会滚动.太棒了.

如果我单击组合框列中的单元格,鼠标滚轮将滚动组合框中的项目.我需要滚动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?

解决方法:

您是否考虑过处理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仍然被删除时,那将是一个不同的情况,但根据你在这里所说的来判断:

If I click on a cell in the combobox column, the mousewheel will
scroll the items in the combobox.

我假设您只是想在选择完成后更改焦点.

上一篇:Linux c++ vim环境搭建系列(3)——Ubuntu18.04.4编译安装youcompleteme


下一篇:MPU+MCU的优势