dataGridView组合框事件处理程序问题 [英] dataGridView ComboBox Event Handler Problem

查看:482
本文介绍了dataGridView组合框事件处理程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题处理一个索引更改事件的comboBox驻留在dataGridView内。我写一个方法来处理comboBox选择更改使用委托:

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

或一个EventHandler:

  comboBox.SelectedIndexChanged + = new EventHandler(ComboBoxIndexChanged); 

,但这两种方法都无法正常工作。也就是说,当你点击comboBox(包含在dataGridView)中的选择,它需要多次点击,导致我的ComboBoxIndexChanged();方法来正常工作,如果它决定工作。在dataGridView中的组合框的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)
{
//做一些奇怪的事情...
}



我在StackOverFlow上读了一个类似的线程,说明处理组合框更改事件这种方式,但我不能得到解决方案工作。可以在此处找到该信息:SelectedIndexChanged事件在Datagridview的ComboBoxColumn 。它说:



事情变得复杂,因为他们通过对所有行只有一个编辑控件来优化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)
{
//处理组合更改的事件
EventHandler comboDelegate = new EventHandler $ b(cbSender,args)=>
{
DoSomeStuff();
});

//使用编辑控件注册事件
combo.SelectedValueChanged + = comboDelegate;

//因为我们不想多次添加此事件,当
//编辑控件被隐藏时,我们必须删除我们添加的处理程序。
EventHandler visibilityDelegate = null;
visibilityDelegate = new EventHandler(
(visSender,args)=>
{
//当编辑控件为
时删除处理程序//不再可见。
if((visSender as Control).Visible == false)
{
combo.SelectedValueChanged - = comboDelegate;
visSender.VisibleChanged - = visibilityDelegate;
}
});

(sender as DataGridView).EditingControl.VisibleChanged + =
visibilityDelegate;我这里的这个问题是,VisSender是一个非常简单的方法, 未定义,因此无法使用事件VisibleChanged。



任何来自您的帮助,一如既往,非常感激。

解决方案

听起来您希望在用户更改下拉框时立即提交更改,而不必单击单元格。这样您就需要在发生更改时强制提交(使用 $ c>:

  //此事件处理程序通过调用CommitEdit方法手动引发CellValueChanged事件
//。
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if(dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}

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


I am having a problem with the handling of an index changed event for a comboBox that resides inside a dataGridView. I write an method to handle the comboBox selection change using either a delegate:

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

or an EventHandler:

comboBox.SelectedIndexChanged += new EventHandler(ComboBoxIndexChanged);

but both methods do not work as expected. That is, when you click on your selection within the comboBox (contained within the dataGridView) it takes multiple clicks to cause my ComboBoxIndexChanged(); method to function proper, that if it decides to function at all. What is the best way to overcome/go-about specifying an event on an indexedChange of a comboBox within a dataGridView?

The code I am currently using in context is as follows:

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

and the event ComboBoxIndexChanged is:

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

I have read a similar thread on StackOverFlow which states that there is a problem with dealing with the comboBox change event this way, but I cannot get the solution to work. The post can be found here: "SelectedIndexChanged" event in ComboBoxColumn on Datagridview. It says:

"Things get complicated since they optimized the DataGridView by only having one editing control for all the rows. Here's how I handled a similar situation:

First hook up a delegate to the EditControlShowing event:

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

Then in the handler, hook up to the EditControl's SelectedValueChanged event:

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;
    }
}"

This issue I have with this is that "VisSender" is not defined hence the event "VisibleChanged" cannot be used.

Any help from you lads, is as always, most appreciated.

解决方案

Sounds like you want the changes to be committed as soon as the user changes the drop down box, without them having to click off of the cell. In order to do this you will need to force the commit when the change happens (using CommitEdit, there is also an example on the MSDN page). Add this to your 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);
    }
}

Then you could just listen for the CellValueChanged and avoid having to try and register for the ComboBoxValueChanged event on the underlying editing control.

这篇关于dataGridView组合框事件处理程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆