user1002716
user1002716

Reputation:

Combobox textChanged event not firing

We have customar table contains Cust_ID, Cust_Name etc..... for this table Cust_Name is not unique and one Customer name can repeat Number of times.

i am getting data from SQL and binding to ComboBox (winform)

 cmbCustomar.Datasource = GetCustomerData(_LocationID);
 cmbCustomar.DisplayMember = "Cust_Name";
 cmbCustomar.ValueMember = "Cust_ID";

Here the Problem is :

Customer Name : JOHN is repeated 4 times, all Cust_ID are different when user select JOHN on first Item i am getting correct "SelectedValue"

but if user select 2 nd or 3rd JOHN Combobox Item it allways default select First Item (Name as JOHN) and the SelectedValue allways return the First Item Value.

i am not able to find where i am doing wrong, please suggest.

Upvotes: 1

Views: 6200

Answers (3)

LarsTech
LarsTech

Reputation: 81675

Try changing the following property:

cmbCustomar.DropDownStyle = DropDownList;

If your ComboBox has DropDownStyle = DropDown, then the "text" part of the ComboBox is trying to match the first item it can find in the list, and in this case, it ignores the current selected item and finds the first "John" on your list.

Upvotes: 1

Sandy
Sandy

Reputation: 11727

It seems that you are very new to it. And as by my guess, because of your incomplete description, you are trying return the "Selected Value" based on the selected text of the combobox. But rather than that you must try to attach a value a to selected text and return that value. It will surely solve your problem.

Hope it helps.

Upvotes: 0

ClearLogic
ClearLogic

Reputation: 3692

Keep in mind "SelectedValueChanged" event will fire when combobox being populated. Make sure to un-subscribe to this event before populating the combobox. and subscribe again after populating the data.

            //unsubsribe the event before populating combobox1
        this.cmbCustomar.SelectedValueChanged -= new System.EventHandler(this.cmbCustomar_SelectedValueChanged);

        cmbCustomar.Datasource = GetCustomerData(_LocationID);
        cmbCustomar.DisplayMember = "Cust_Name";
        cmbCustomar.ValueMember = "Cust_ID";

        //subscribe the event again 
        this.cmbCustomar.SelectedValueChanged += new System.EventHandler(this.cmbCustomar_SelectedValueChanged);

Upvotes: 1

Related Questions