2011-11-27 45 views
0

嗨,在我的组合框中,我希望不同的国家出现在它中。但我努力尝试,永远不要让它出现。这就是我所做的:不能让组合框对象出现

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; 
    } 

我能做些什么,我仍然不明白的组合框内部的任何国家?!?

+0

您提供的代码看起来很不寻常 - 是一个windows窗体类吗?为什么你在组合框选择的索引更改事件中设置组合框数据源?此外,组合框的显示成员属性还会选择要显示的数据绑定类型的属性,这看起来并不像您尝试使用它的方式。 –

回答

2

您提供的代码并没有多大意义,只有一个类包含相同类的列表并且具有name属性的国家。 (我有点惊讶它的所有编译)

我将给出两个基本的例子,提供数据到WinForms中的组合框 - 第一个简单提供一个字符串列表和第二个数据绑定对象列表(我怀疑这是你的目标)。

下面是一个Form类,它有一个ComboBox成员。我创建了一个列表,并提供国名,然后出现在组合框:

public partial class Form1 : Form 
{ 
    private List<string> countries_; 

    public Form1() 
    { 
     InitializeComponent(); 

     countries_ = new List<string> { "UK", 
           "Australia", 
           "France" }; 

     comboBox1.DataSource = countries; 

    } 
} 

下一个例子是,除了现在很相似我结合型国家的一个列表,其中国家是一个id类(可能来自一个数据库)和一个名字。我将显示成员设置为告诉组合框显示用户的类属性,并将值成员设置为id属性以允许我设置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; } 
} 
0

您在您的SelectedIndexChanged事件中调用Countries类的default constructor。所以,我的行为似乎很自然,因为城市列表中不包含任何项目。您是否需要致电parameterized constructor填写您的收藏?

+0

你是什么意思 – user1067973

+0

在你的SelectionChanged事件中,你创建一个对象为 - CustomerFiles.Countries country = new CustomerFiles.Countries(); 你是否需要这样打电话 - CustomerFiles.Countries country = new CustomerFiles.Countries(“CountryName”); –

2

你需要调用DataBind方法

cbCountry.DataSource = country.Cities; 
cbCountry.DisplayMember = country.Name; 

cbCountry.DataBind(); // method is absent in WinForms 

更新:你的问题是不是与数据绑定 - 你可以在你的表格填写的构造cbCountry.DataSource。您的Countries.Cities属性为空。

+0

on“cb.Country.DisplayMember = country.Cities”我得到这个错误:不能隐式地将类型'System.Collections.Generic.IList '转换为'字符串' 和“cbCountry.Databind ();”我得到这个错误:不可调用的成员'System.Windows.Forms.Control.DataBindings'不能像方法一样使用。 – user1067973

+0

With cb.Country.DisplayMember = country.Cities - 我的错(错误地复制了代码) – Vitaliy

+0

winforms combobox没有DataBind方法。您正在考虑webforms。 –