Set SelectedItem on a combobox bound to datasource

Go To StackoverFlow.com

11

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

2012-04-04 17:04
by mdc


13

You should do

comboBox.SelectedValue = "valueToSelect";

or

comboBox.SelectedIndex = n;

or

comboBox.Items[n].Selected = true;
2012-04-04 17:05
by Claudio Redi
comboBox.Items[n].Selected = true; doesnt work for me (might be a CF issue) but SelectedValue does, I tried it before but with the wrong value. Thanks - mdc 2012-04-04 18:45
I would like to note that for me to get this to work I had to specify the field that was the value member, not just the object. So in the case of customers above, I had to use comboBox.SelectedValue = customerToSelect.id - AdamMc331 2015-07-21 15:42


2

Your binding code is not complete. Try this:

BindingSource bsCustomers = new BindingSource();
bsCustomers.DataSource = _customers;

comboBox.DataBindings.Add(
    new System.Windows.Forms.Binding("SelectedValue", bsCustomers, "id", true));
comboBox.DataSource = bsCustomers;
comboBox.DisplayMember = "name";
comboBox.ValueMember = "id";

In most cases you can accomplish this task in the designer, instead of doing it in code.

Start by adding a data source in the "Data Sources" window in Visual Studio. Open it from menu View > Other Windows > Data Sources. Add an Object data source of Customer type. In the Data Sources you will see the customer's properties. Through a right click on the properties you can change the default control associated to it.

Now you can simply drag a property from the Data Sources window to you form. Visual Studio automatically adds A BindingSource and a BindingNavigator component to your form when you drop the first control. The BindingNavigator is optional and you can safely remove it, if you don't need it. Visual Studio also does all the wire-up. You can tweak it through the properties window. Sometimes this is required for combo boxes.

There's only one thing left to do in your code: Assign an actual data source to the binding source:

customerBindingSource.DataSource = _customers;
2012-04-04 17:17
by Olivier Jacot-Descombes
With that it crasches on comboBox.ValueMember = "id"; for some reason - mdc 2012-04-04 17:51
I suggest you to add the BindingSource as a component to your form in the designer (see Data section of the Toolbox). Then you can set all these properties through the properties window. It is even easier, if you start by defining an object data source in the Data Sources window in VS. Then you can simply drag the fields from this window to your form and the binding-wire-up is done automatically. A BindingSource and a BindingNavigator are inserted automatically, if you do so. Then, you can safely remove the BindingNavigator if you do not need it - Olivier Jacot-Descombes 2012-04-05 14:29


0

this works for me

bsCustomers.Position = comboBox.Items.IndexOf(targetCustomer);
2019-01-31 10:53
by Raihan Al-maMun
Ads