How to prevent selectedindexchanged event when DataSource is bound?

随声附和 提交于 2019-12-18 12:13:15

问题


I have ComboBox control(WinForm project).

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

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


回答1:


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:

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


        // Set your bindings here . . .


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



回答2:


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

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

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




回答3:


Use SelectionChangeCommitted Event instead of 'SelectedIndexChanged'

SelectionChangeCommitted is raised only when the user changes the combo box selection. Do not use SelectedIndexChanged or SelectedValueChanged to capture user changes, because those events are also raised when the selection changes programmatically.

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




回答4:


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).




回答5:


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.

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


来源:https://stackoverflow.com/questions/14111879/how-to-prevent-selectedindexchanged-event-when-datasource-is-bound

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!