Reputation: 41
Hi in my combobox i want different countries to appear in it. But i try and try and never gets it to appear. This is how i have done:
class Countries
{
public string Name { get; set; }
public IList<Countries> Cities { get; set; }
public Countries()
{
}
public Countries(string _name)
{
Cities = new List<Countries>();
Name = _name;
List<Countries> countries = new List<Countries> { new Countries("UK"),
new Countries("Australia"),
new Countries("France") };
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
CustomerFiles.Countries country = new CustomerFiles.Countries();
cbCountry.DataSource = country.Cities;
cbCountry.DisplayMember = country.Name;
}
What can i do i still dont get any countries inside the combobox?!?
Upvotes: 0
Views: 313
Reputation: 33183
The code you have provided doesn't make a great deal of sense, with a class Countries which contains a list of the same class and then has the name property. (I'm a little surprised it all compiles)
I'm going to give two basic examples of providing data to a combobox in WinForms - the first simply providing a list of strings and the second databinding against a list of objects (which I suspect is your goal).
Below is a Form class which has a ComboBox member. I create a List and provide the country names which then appear in the ComboBox:
public partial class Form1 : Form
{
private List<string> countries_;
public Form1()
{
InitializeComponent();
countries_ = new List<string> { "UK",
"Australia",
"France" };
comboBox1.DataSource = countries;
}
}
The next example is very similar except now I bind to a list of type Country where Country is a class with an id (possibly from a database) and a name. I set the display member to tell the combobox what class property to show the user, and set the value member to the id property to allow me to set the SelecteValue.
public partial class Form1 : Form
{
bool click = false;
public Form1()
{
InitializeComponent();
List<Country> countries = new List<Country> { new Country{ CountryId = 1, Name = "UK"},
new Country{ CountryId = 2, Name = "Australia"},
new Country{ CountryId = 3, Name = "France"} };
comboBox1.DataSource = countries;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "CountryId";
comboBox1.SelectedValue = 2;
}
}
public class Country
{
public int CountryId { get; set; }
public string Name { get; set; }
}
Upvotes: 2
Reputation: 2804
You need to call DataBind method
cbCountry.DataSource = country.Cities;
cbCountry.DisplayMember = country.Name;
cbCountry.DataBind(); // method is absent in WinForms
Update: your problem is not with databinding - you can fill cbCountry.DataSource in your Form constructor. Your Countries.Cities property is empty.
Upvotes: 2
Reputation: 81313
You are calling the default constructor
of Countries class in your SelectedIndexChanged event. So, the behaviour seems natural to me, since Cities list doesn't contain any item. Don't you need to call your parameterized constructor
to fill your collection?
Upvotes: 0