views:

5202

answers:

3

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

A: 

Try something like this:

yourControl.DataSource = countryInstance.Cities;

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

yourControl.DataBind();
Andrew Hare
that doesn't use a bindingsource...
Mitch Wheat
Thats what i was asking for???
Mobin
as well as comboBox1.DataBind(); function i dont see it in solutionsI am using winforms
Mobin
+6  A: 

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";
Mitch Wheat
thanks but a bit of a problem here the Names are not visible in the combobox when running the application
Mobin
I just created a winforms app with exactly that code and it works fine.
Mitch Wheat
@Mobin If the names are not visible make sure that you are binding the DisplayMember to a Property on the class and not a public field. If you class uses `public string Name { get; set; }` it will work but if it uses `public string Name;` it will not be able to access the value and instead will display the object type for each line in the combo box.
Greg Bray
...and to find the country selected in the bound combobox, you would do something like: `Country country = (Country)comboBox1.SelectedItem;`
demoncodemonkey
...which might seem obvious but then everything is obvious in hindsight :)
demoncodemonkey
A: 
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

Mobin
A few Notes: A bindingSource is a kind of link-through source, you are not really using it now, probably Ok. But if you want to uses it to link up other stuff, use member_cbx = bindingSource1;
Henk Holterman