How to bind a List to a ComboBox?

2019-01-01 15:42发布

问题:

I want to connect a BindingSource to a list of class objects and then objects value to a ComboBox.
Can anyone suggest how to do it?

public class Country
{
    public string Name { get; set; }
    public IList<City> Cities { get; set; }

    public Country()
    {
        Cities = new List<City>();
    }
}

is my class and I want to bind its name field to a BindingSource which could be then associated with a ComboBox

回答1:

As you are referring to a combobox, I\'m assuming you don\'t want to use 2-way databinding (if so, look at using a BindingList)

public class Country
{
    public string Name { get; set; }
    public IList<City> Cities { get; set; }
    public Country(string _name)
    {
        Cities = new List<City>();
        Name = _name;
    }
}



List<Country> countries = new List<Country> { new Country(\"UK\"), 
                                     new Country(\"Australia\"), 
                                     new Country(\"France\") };

bindingSource1.DataSource = countries;

comboBox1.DataSource = bindingSource1.DataSource;

comboBox1.DisplayMember = \"Name\";
comboBox1.ValueMember = \"Name\";


回答2:

For a backgrounder, there are 2 ways to use a ComboBox/ListBox

1) Add Country Objects to the Items property and retrieve a Country as Selecteditem. To use this you should override the ToString of Country.

2) Use DataBinding, set the DataSource to a IList (List<>) and use DisplayMember, ValueMember and SelectedValue

For 2) you will need a list of countries first

// not tested, schematic:
List<Country> countries = ...;
...; // fill 

comboBox1.DataSource = countries;
comboBox1.DisplayMember=\"Name\";
comboBox1.ValueMember=\"Cities\";

And then in the SelectionChanged,

if (comboBox1.Selecteditem != null)
{
   comboBox2.DataSource=comboBox1.SelectedValue;

}


回答3:

public MainWindow(){
    List<person> personList = new List<person>();

    personList.Add(new person { name = \"rob\", age = 32 } );
    personList.Add(new person { name = \"annie\", age = 24 } );
    personList.Add(new person { name = \"paul\", age = 19 } );

    comboBox1.DataSource = personList;
    comboBox1.DisplayMember = \"name\";

    comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
}


void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    person selectedPerson = comboBox1.SelectedItem as person;
    messageBox.Show(selectedPerson.name, \"caption goes here\");
}

boom.



回答4:

Try something like this:

yourControl.DataSource = countryInstance.Cities;

And if you are using WebForms you will need to add this line:

yourControl.DataBind();


回答5:

public class Country
{
    public string Name { get; set; }
    public IList<City> Cities { get; set; }

    public Country()
    {
        Cities = new List<City>();
    }
}

public class City 
{
    public string Name { get; set; } 
}

List<Country> Countries = new List<Country>
{
    new Country
    {
        Name = \"Germany\",
        Cities =
        {
            new City {Name = \"Berlin\"},
            new City {Name = \"Hamburg\"}
        }
    },
    new Country
    {
        Name = \"England\",
        Cities =
        {
            new City {Name = \"London\"},
            new City {Name = \"Birmingham\"}
        }
    }
};
bindingSource1.DataSource = Countries;
member_CountryComboBox.DataSource = bindingSource1.DataSource;
member_CountryComboBox.DisplayMember = \"Name\";
member_CountryCombo

Box.ValueMember = \"Name\";

This is the code I am using now.



回答6:

If you are using a ToolStripComboBox there is no DataSource exposed (.NET 4.0):

List<string> someList = new List<string>();
someList.Add(\"value\");
someList.Add(\"value\");
someList.Add(\"value\");

toolStripComboBox1.Items.AddRange(someList.ToArray());