2013-02-13 96 views
2

在此加載函數中有一些酒店名稱,我想將該酒店名稱綁定到組合框。我去了幾個步驟,但我有一個綁定值從這裏組合框的問題。將數據綁定到組合框

private void myWindow_Load(object sender, EventArgs e) 
{ 
    string new1 = f.Name; //hotel names comes here(eg:hilton,serandib) 

    List<string> Hotels = new1 List<string>(); 
    Hotels.Add(new1); 
    foreach (string Hotel in Hotels) 
    { 

    } 
} 

其實我想要這個酒店名稱顯示在組合框中(這是一個窗體),幫助我休息。

+0

什麼是錯誤?我認爲新是保留關鍵字,你不能用它作爲變量名! – saeed 2013-02-13 05:44:19

+0

http://stackoverflow.com/questions/3768328/c-sharp-combobox-binding-to-list-of-objects – user2046210 2013-02-13 06:09:04

回答

2

您可以使用下面的代碼,

ComboBox1.DataSource = hotelList; 

如果你有下面的字符串從f.Name

「樂經絡,財富,韓亞」

List<String> hotelList = f.Name.Split(',').ToList(); 

    ComboBox1.DataSource = hotelList; 
+0

不,它一個接一個,不需要拆分它 – 2013-02-13 05:45:52

+0

只需收集列表中的所有名稱並綁定它..如前所述! – 2013-02-13 05:48:46

1
List<Hotels> Hname = new List<Hotels> { "Taj", " Star", "Poorna" ,"Settinad" }; 
comboBox.DataSource = Hname; 

或來

List<Hotels> Hotel = new List<Hotels>(); 
Hotel.Add("name"); 

comboBox.DataSource = Hotel; 
1
 List<string> names = new List<string>(); 
     names.Add("name1"); 
     names.Add("name2"); 
     names.Add("name3"); 
     names.Add("name4"); 
     names.Add("name5"); 
     comboBox1.Items.Clear(); 
     foreach (string name in names) 
     { 
      comboBox1.Items.Add(name); 
     } 
     comboBox1.SelectedIndex = 0; //selects first item 
1

您即將到一個項目添加到ComboBox但實際上你並不需要使用List<string>列出的項目ComboBox,你可以直接去它.ItemsComboBox

string new1 = f.Name; //hotel names comes here(eg:hilton,serandib) 
comboBox5.Items.Add(new1);