jak powiązać listę z comboboxem? (Winforms)

Chcę podłączyć wiążące źródło do listy obiektów klasy, a następnie wartość obiektów do pola kombi czy ktoś może zasugerować, Jak to zrobić

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

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

Jest moją klasą i chcę powiązać jej pole nazwa z wiążącym źródłem, które może być następnie powiązane z combobox

Author: Mobin, 2009-03-02

6 answers

Ponieważ odnosisz się do comboboxu, zakładam, że nie chcesz używać dwukierunkowej bazy danych (jeśli tak, spójrz na użycie 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";
 133
Author: Mitch Wheat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-24 16:12:43

Dla backgroundera istnieją 2 sposoby użycia ComboBox / ListBox

1) Dodaj obiekty Country do Właściwości Items i pobieraj kraj jako Selecteditem. Aby tego użyć, należy zastąpić ToString kraju.

2) Użyj Databindingu, Ustaw źródło danych na IList (List) i użyj DisplayMember, ValueMember i SelectedValue

Dla 2) najpierw będziesz potrzebował listy krajów

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

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

A następnie w SelectionChanged,

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

}
 21
Author: Henk Holterman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-02 00:29:53
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.
 19
Author: Robert Tonnessen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-12 21:36:40

Spróbuj czegoś takiego:

yourControl.DataSource = countryInstance.Cities;

I jeśli używasz WebForms, musisz dodać tę linię:

yourControl.DataBind();
 0
Author: Andrew Hare,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-02 00:03:18
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";

To jest kod, którego teraz używam

 0
Author: Mobin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-02 00:38:09

Jeśli używasz ToolStripComboBox, nie ma odsłoniętego źródła danych (. NET 4.0):

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

toolStripComboBox1.Items.AddRange(someList.ToArray());
 -2
Author: John M,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-24 19:30:02