如何将列表绑定到组合框?(Winforms)

时间:2021-08-07 20:53:53

I want to connect a binding source to a list of class objects and then objects value to a combo box 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 binding source which could be then associated with a combobox

我的类和我是否想将它的name字段绑定到绑定源,然后绑定源可以与combobox相关联

6 个解决方案

#1


132  

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)

当您在引用combobox时,我假设您不希望使用双向数据库(如果需要,请查看使用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


22  

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

对于一个背景,有两种使用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.

1)将Country对象添加到Items属性中,并将一个国家检索为Selecteditem。要使用这个,您应该重写国家的ToString。

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

2)使用DataBinding,将数据源设置为IList (List<>),并使用DisplayMember、ValueMember和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


17  

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


0  

Try something like this:

试试这样:

yourControl.DataSource = countryInstance.Cities;

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

如果您正在使用WebForms,您需要添加以下内容:

yourControl.DataBind();

#5


0  

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_CountryComboBox.ValueMember = "Name";

This is the code i am using now

这就是我现在使用的代码

#6


-2  

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

如果您正在使用ToolStripComboBox,则没有公开的数据源(。NET 4.0):

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

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

#1


132  

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)

当您在引用combobox时,我假设您不希望使用双向数据库(如果需要,请查看使用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


22  

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

对于一个背景,有两种使用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.

1)将Country对象添加到Items属性中,并将一个国家检索为Selecteditem。要使用这个,您应该重写国家的ToString。

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

2)使用DataBinding,将数据源设置为IList (List<>),并使用DisplayMember、ValueMember和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


17  

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


0  

Try something like this:

试试这样:

yourControl.DataSource = countryInstance.Cities;

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

如果您正在使用WebForms,您需要添加以下内容:

yourControl.DataBind();

#5


0  

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_CountryComboBox.ValueMember = "Name";

This is the code i am using now

这就是我现在使用的代码

#6


-2  

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

如果您正在使用ToolStripComboBox,则没有公开的数据源(。NET 4.0):

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

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