user42348
user42348

Reputation: 4319

Concerned with selectedindexchanged of dropdownlist

I have a combobox in which iam loading combobox using following code.

public void LoadCustomer1(ComboBox pCmbCustomer)
        {
            obj._dtInputParameter.Clear();
            obj.AddInputParameter("@Prm_OpFlag", "S", "String", 1);
            //obj.strSPName = "prc_CUST_Details";
            obj.strSPName = "EditCustCombo";
            DataSet ds = obj.SqlExecuteFill();
            pCmbCustomer.DataSource = ds.Tables[0];
            pCmbCustomer.DisplayMember = "CustomerId";
            pCmbCustomer.ValueMember = "CustomerId";
            pCmbCustomer.Text = "--- Select Customer Id ---";
            pCmbCustomer.SelectedIndex = 0;

        }

Problem is in pCmbCustomer.DataSource = ds.Tables[0];selected indexchanged event of combobox is working.how can i avoid selected indexchanged event while binding combobox?Can anybody help?

Upvotes: 0

Views: 391

Answers (3)

pdiddy
pdiddy

Reputation: 6297

I would use the SelectionChangeCommitted instead of using the SelectedIndexChange if possible.

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

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

Upvotes: 1

user366312
user366312

Reputation: 17008

Avoid handling selectedIndexChangedEvent as mush as possible.

Avoid using pCmbCustomer.Text = "--- Select Customer Id ---"; this type of statements. That is, do not set the text explicitly.

Make the text "--- Select Customer Id ---" as a member of the list.

Then use this pCmbCustomer.SelectedIndex = 0,2,3...,n; statement when you need.

Upvotes: 1

Jamie Dixon
Jamie Dixon

Reputation: 54011

You can attach to the SelectedIndexChanged event handler once you've finished binding the combobox.

So instead of attaching the event directly in the user control, attach it in your codebehind either at the end of LoadCustomer1 or outside of it after LoadCustomer1 has been called.

Upvotes: 1

Related Questions