Set SelectedItem on a combobox bound to datasource

mdc picture mdc · Apr 4, 2012 · Viewed 41.3k times · Source
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...

Answer

Claudio Redi picture Claudio Redi · Apr 4, 2012

You should do

comboBox.SelectedValue = "valueToSelect";

or

comboBox.SelectedIndex = n;

or

comboBox.Items[n].Selected = true;