user1774447
user1774447

Reputation: 33

Combobox Datasource not creating items

After much searching I have still not found a solution to this.

I have created a list in VS 2010 and bound it to a ComboBox. The DataSource property shows the entire list but the combobox on the form is empty, as is the items property of the box.

    private List<string> classes = new List<string>();
    private BindingList<string> bindingClasses;

        classes.Add("Spinning");
        classes.Add("Step");
        classes.Add("Pilates");
        classes.Add("Kickboxing");
        classes.Add("Body Sculpting");

        bindingClasses = new BindingList<string>(classes);
        classesComboBox.DataSource = bindingClasses;

I have also tried adding

classesComboBox.DisplayMember = "Spinning";

and without the bindingList between to no avail.

Why is this not working?

Upvotes: 3

Views: 2257

Answers (2)

Cole Cameron
Cole Cameron

Reputation: 2233

Lose the BindingList, just set the DataSource to the List<String> itself.

List<string> classes = new List<string>();
classes.Add("Spinning");
classes.Add("Step");
classes.Add("Pilates");
classes.Add("Kickboxing");
classes.Add("Body Sculpting");

classesComboBox.DataSource = classes;
classesComboBox.SelectedItem = "Spinning";

Upvotes: 1

Haedrian
Haedrian

Reputation: 4328

classesComboBox.DataBind();

You need to specify when it should grab the data.

Upvotes: 0

Related Questions