List<Customer> _customers = getCustomers().ToList();
BindingSource bsCustomers = new BindingSource();
bsCustomers.DataSource = _customers;
comboBox.DataSource = bsCustomers.DataSource;
comboBox.DisplayMember = "name";
comboBox.ValueMember = "id";
Now how do I set the combobox's Item to something other than the first in the list? Tried comboBox.SelectedItem = someCustomer; ...and lots of other stuff but no luck so far...
When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index.
C# ComboBox is a combination of a TextBox and a ListBox control. Only one list item is displayed at one time in a ComboBox and other available items are loaded in a drop down list.
You should do
comboBox.SelectedValue = "valueToSelect";
or
comboBox.SelectedIndex = n;
or
comboBox.Items[n].Selected = true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With