28

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?

Michael
  • 13,950
  • 57
  • 145
  • 288
  • 1
    Could you set a flag, set the DataSource, and unset the flag? Then in your event handler you could see the flag is set and simply not take action on it. – prprcupofcoffee Jan 01 '13 at 16:03
  • 3
    I found solution for my case here: http://stackoverflow.com/a/3268120/10679430 – Michael Jan 01 '13 at 16:32
  • possible duplicate of [Stop comboBox's selectedIndexChanged event from firing when the form loads](http://stackoverflow.com/questions/3263240/stop-comboboxs-selectedindexchanged-event-from-firing-when-the-form-loads) – Andre Miras Apr 21 '15 at 13:42

5 Answers5

53

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);
}
XIVSolutions
  • 4,442
  • 19
  • 24
40

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.

Community
  • 1
  • 1
shreesha
  • 1,811
  • 2
  • 21
  • 30
  • 1
    While the accepted answer worked as intended, I wish I read this one first. It is a lot simpler to implement, and a heck of a lot simpler to read for others afterwards... So thank you, two year old post! – Jakob Busk Sørensen Jun 14 '17 at 08:27
  • 1
    This should have been the correct answer in my opinion. The above answer works but in an application in which you'd have multiple combo boxes and data sources can you imagine having to unsubscribe and resubscribe every time you want to mess with the data source? Thanks @shreesha – Sam Williams Sep 26 '17 at 13:32
15

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

3

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

paparazzo
  • 44,497
  • 23
  • 105
  • 176
0

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);
René Vogt
  • 43,056
  • 14
  • 77
  • 99
Rasulbek
  • 1
  • 1