Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set SelectedItem on a combobox bound to datasource

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...

like image 741
mdc Avatar asked Apr 04 '12 17:04

mdc


People also ask

What is SelectedItem in ComboBox?

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.

What is a ComboBox in C#?

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.


1 Answers

You should do

comboBox.SelectedValue = "valueToSelect";

or

comboBox.SelectedIndex = n;

or

comboBox.Items[n].Selected = true;
like image 123
Claudio Redi Avatar answered Sep 19 '22 17:09

Claudio Redi