Reputation:
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
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
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
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