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

mdc
  • 1,161
  • 6
  • 22
  • 37

3 Answers3

14

You should do

comboBox.SelectedValue = "valueToSelect";

or

comboBox.SelectedIndex = n;

or

comboBox.Items[n].Selected = true;
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • 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 Apr 04 '12 at 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 Jul 21 '15 at 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;
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • With that it crasches on comboBox.ValueMember = "id"; for some reason? – mdc Apr 04 '12 at 17:51
  • 2
    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 Apr 05 '12 at 14:29
  • which property or object represent the bound selected item? isn't bsCustomers a list and it has multiple "id" – joe Jun 08 '21 at 01:20
  • The data source is a `List` and `Customer` has `id` and `name` properties. The `Customer.id` is bound to the `ComboBox.SelectedValue` property. – Olivier Jacot-Descombes Jun 08 '21 at 12:49
1

this works for me

bsCustomers.Position = comboBox.Items.IndexOf(targetCustomer);
Raihan Al-Mamun
  • 337
  • 6
  • 11