将数据绑定到组合框
问题描述:
在此加载函数中有一些酒店名称,我想将该酒店名称绑定到组合框。我去了几个步骤,但我有一个绑定值从这里组合框的问题。将数据绑定到组合框
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)
{
}
}
其实我想要这个酒店名称显示在组合框中(这是一个窗体),帮助我休息。
答
您可以使用下面的代码,
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
答
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;
答
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
答
您即将到一个项目添加到ComboBox
但实际上你并不需要使用List<string>
列出的项目ComboBox
,你可以直接去它.Items
的ComboBox
string new1 = f.Name; //hotel names comes here(eg:hilton,serandib)
comboBox5.Items.Add(new1);
什么是错误?我认为新是保留关键字,你不能用它作为变量名! – saeed 2013-02-13 05:44:19
http://stackoverflow.com/questions/3768328/c-sharp-combobox-binding-to-list-of-objects – user2046210 2013-02-13 06:09:04