Michael
Michael

Reputation: 13636

How to prevent selectedindexchanged event when DataSource is bound?

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?

Upvotes: 29

Views: 45483

Answers (5)

Arpit Shrivastava
Arpit Shrivastava

Reputation: 396

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

Upvotes: 17

Rasulbek
Rasulbek

Reputation: 1

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

Upvotes: 0

shreesha
shreesha

Reputation: 1881

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.

Upvotes: 41

XIVSolutions
XIVSolutions

Reputation: 4502

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

Upvotes: 56

paparazzo
paparazzo

Reputation: 45106

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

Upvotes: 3

Related Questions