2011-11-27 48 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。 –