Combobox dropdownlist with duplicate items  
Author Message
Goos van Beek





PostPosted: Windows Forms Designer, Combobox dropdownlist with duplicate items Top

I have a bound combobox wich shows duplicate DisplayMembers, but with different ValueMembers. Think of a list with invoices where one person has several invoices. His name is several times in the list. When the dropdownlist opens, it always select the first of the DisplayMembers (for that name), although another DisplayMember (and ValueMember) was chosen before.

This is a part of the code I use to bind the data to the combobox:

DataSet ds = new DataSet();
cn.Open();
adapter.Fill(ds, "dbo.GetNamesAndInvoiceList");
cn.Close();
this.cboInvoiceNames.DataSource = ds.Tables[0];
this.cboInvoiceNames.DisplayMember = "DisplayName".ToString();
this.cboInvoiceNames.ValueMember = "PK_InvoiceID".ToString();

How can I change this behaviour.

Thanks in advance,

Goos van Beek




Windows Forms34  
 
 
Peter Ritchie





PostPosted: Windows Forms Designer, Combobox dropdownlist with duplicate items Top

Is your ComboBox's DropDownStyle property set to DropDownList instead of DropDown

When a ComboBox's DropDownStyle is set to DropDown, the selected items value is placed in the edit box for the ComboBox and that value is then used to compare the display text of the other items in the dropdown to decide which is the selected item. This is because the user can type text into the combobox's edit box. When you set the style to DropDownList, only values in the list can be selected, and therefore the items values are compared, not their text.



 
 
Goos van Beek





PostPosted: Windows Forms Designer, Combobox dropdownlist with duplicate items Top

Thanks Peter.

I didn't know it was that easy :-)