dataGridView ComboBox事件处理程序问题

时间:2011-08-25 16:26:13

标签: c# winforms datagridview combobox event-handling

我遇到了处理位于dataGridView内的comboBox的索引更改事件的问题。我编写了一个方法来使用委托来处理comboBox选择更改:

ComboBox.SelectedIndexChanged -= delegate { ComboBoxIndexChanged(); };
ComboBox.SelectedIndexChanged += delegate { ComboBoxIndexChanged(); };

或EventHandler:

comboBox.SelectedIndexChanged += new EventHandler(ComboBoxIndexChanged);

但两种方法都无法按预期工作。也就是说,当您在comboBox中单击您的选择(包含在dataGridView中)时,需要多次单击才能生成我的ComboBoxIndexChanged();正常运行的方法,如果它决定完全起作用。克服/关于在dataGridView中的comboBox的indexedChange上指定事件的最佳方法是什么?

我目前在上下文中使用的代码如下:

private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    try
    {
        if (this.dataGridView.CurrentCell.ColumnIndex == (int)Column.Col)
        {
            ComboBox comboBox = e.Control as ComboBox;
            if (comboBox != null)
            {
                comboBox.SelectedIndexChanged += new EventHandler(ComboBoxIndexChanged);
            }
        }
        return;
    }
    catch (Exception Ex)
    {
        Utils.ErrMsg(Ex.Message);
        return;
    }
}

并且事件ComboBoxIndexChanged是:

private void ComboBoxIndexChanged(object sender, EventArgs e)
{
    // Do some amazing stuff...
}

我在StackOverFlow上读过一个类似的线程,它表示以这种方式处理comboBox更改事件存在问题,但我无法使解决方案正常工作。该帖子可在此处找到:"SelectedIndexChanged" event in ComboBoxColumn on Datagridview。它说:

“事情变得复杂,因为他们通过对所有行只有一个编辑控件来优化DataGridView。这就是我处理类似情况的方式:

首先将委托连接到EditControlShowing事件:

myGrid.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(
                                Grid_EditingControlShowing);
...

然后在处理程序中,连接到EditControl的SelectedValueChanged事件:

void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox combo = e.Control as ComboBox;
    if (combo != null)
    {
        // the event to handle combo changes
        EventHandler comboDelegate = new EventHandler(
          (cbSender, args) =>
            {
                DoSomeStuff();
            });

        // register the event with the editing control
        combo.SelectedValueChanged += comboDelegate;

        // since we don't want to add this event multiple times, when the 
        // editing control is hidden, we must remove the handler we added.
        EventHandler visibilityDelegate = null;
        visibilityDelegate = new EventHandler(
          (visSender, args) =>
            {
                // remove the handlers when the editing control is
                // no longer visible.
                if ((visSender as Control).Visible == false)
                {
                    combo.SelectedValueChanged -= comboDelegate;
                    visSender.VisibleChanged -= visibilityDelegate;
                }
            });

         (sender as DataGridView).EditingControl.VisibleChanged += 
           visibilityDelegate;
    }
}"

我遇到的这个问题是“VisSender”未定义,因此无法使用“VisibleChanged”事件。

你们的帮助,一如既往,非常感谢。

1 个答案:

答案 0 :(得分:13)

听起来您希望在用户更改下拉框后立即提交更改,而无需单击关闭单元格。为了做到这一点,您需要在发生更改时强制提交(使用CommitEdit,MSDN页面上也有一个示例)。将其添加到您的DataGridView

// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
    EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

然后您可以只听取CellValueChanged并避免尝试在底层编辑控件上注册ComboBoxValueChanged事件。