C# 绑定数据源时如何防止 selectedindexchanged 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14111879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:42:33  来源:igfitidea点击:

How to prevent selectedindexchanged event when DataSource is bound?

c#.netcombobox

提问by Michael

I have ComboBox control(WinForm project).

我有 ComboBox 控件(WinForm 项目)。

When I bind DataSource to the ComboBox control combobox_selectedindexchanged event is fired.

当我将 DataSource 绑定到 ComboBox 控件时,会触发 combobox_selectedindexchanged 事件。

Any idea how to prevent selectedindexchanged event when DataSource is bound?

知道如何在绑定 DataSource 时防止 selectedindexchanged 事件吗?

采纳答案by XIVSolutions

Remove the handler for the SelectedIndex_Changed event, bind your data, then add the handler back. Following is a simple example of how this might be done within a method:

删除 SelectedIndex_Changed 事件的处理程序,绑定您的数据,然后重新添加处理程序。以下是如何在方法中完成此操作的简单示例:

private void LoadYourComboBox()
{
    this.comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChanged);


        // Set your bindings here . . .


    this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}

回答by paparazzo

Don't think you can stop the event but you can not handle it.

不要认为你可以阻止事件,但你无法处理它。

Detach the event handler(s), bind, and then attach event handler(s).

分离事件处理程序,绑定,然后附加事件处理程序。

回答by shreesha

I know this is an old post and it has an accepted answer but i think we can use SelectionChangeCommittedevent as a solution to avoid event firing during databind.

我知道这是一篇旧帖子,它有一个公认的答案,但我认为我们可以使用SelectionChangeCommitted事件作为解决方案,以避免在数据绑定期间触发事件。

SelectionChangeCommitted event fires only when the users change the selection in the combobox.

SelectionChangeCommitted 事件仅在用户更改组合框中的选择时触发。

there is a similar questionon SO and this answer is provided by @arbiter.

在 SO 上有一个类似的问题,这个答案由@arbiter 提供。

回答by Rasulbek

Here is simple way. You may use Tag property of combobox. It can be empty or 0 integer value when it's empty or have not fulfilled yet. You have to set combobox's Tag as count of it's items after bounding. In SelectedValueChanged event if the Tag property is null or 0 you have to return from void.

这里是简单的方法。您可以使用组合框的 Tag 属性。当它为空或尚未满足时,它可以为空或 0 整数值。您必须将组合框的标签设置为边界后的项目数。在 SelectedValueChanged 事件中,如果 Tag 属性为 null 或 0,则必须从 void 返回。

Here is some samples from my project.

这是我项目中的一些示例。

private void cb_SelectedValueChanged(object sender, EventArgs e)
{
    if (!(sender is ComboBox)) return;
    ComboBox cb = sender as ComboBox;
    if (DataUtils.ToInt(cb.Tag, 0) == 0) return;
    if (cbSmk.SelectedValue == null ) return;
    /* Continue working;  */
}

public static void ComboboxFill(ComboBox cb, string keyfld, string displayfld, string sql)
{          
    try
    {
        cb.Tag = 0;
        cb.DataSource = null;
        cb.Items.Clear();

        DataSet ds = DataMgr.GetDsBySql(sql);
        if (!DataUtils.HasDtWithRecNoErr(ds))
        {                    
            cb.Text = "No data";
        }
        else
        {
            cb.DataSource = ds.Tables[0];
            cb.DisplayMember = displayfld;
            cb.ValueMember = keyfld;
        }
        cb.Tag = cb.Items.Count;
    }
    catch (Exception ex)
    {
        Int32 len = ex.Message.Length > 200 ? 200 : ex.Message.Length;
        cb.Text = ex.Message.Substring(0, len);
    }                
}

CmpHelper.ComboboxFill(cbUser, "USER_ID", "USER_NAME", "SELECT * FROM SP_USER WHERE 1=1 ORDER by 1",-1);

回答by Arpit Shrivastava

Use SelectionChangeCommittedEvent instead of 'SelectedIndexChanged'

使用SelectionChangeCommitted事件而不是“SelectedIndexChanged”

SelectionChangeCommittedis raised only when the user changes the combo box selection. Do not use SelectedIndexChangedor SelectedValueChangedto capture user changes, because those events are also raised when the selection changes programmatically.

SelectionChangeCommitted仅在用户更改组合框选择时引发。请勿使用SelectedIndexChangedSelectedValueChanged捕获用户更改,因为在编程方式更改时也会提出这些事件。

FROM https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectionchangecommitted.aspx

来自https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectionchangecommitted.aspx