将字符串列表绑定到组合框
本文关键字:组合 绑定 字符串 列表 | 更新日期: 2023-09-27 18:36:11
我正在将List<String>
绑定到组合框。我用两种方式写了这篇文章。第一种方式,ComboBox 的输出为空。我错在哪里?请帮助我。这是我的代码:
public class MaritalStatusComboBox:ComboBox
{
public MaritalStatusComboBox()
{
BindingSource bs = new BindingSource();
bs.DataSource = new List<string> {"Single","Married" };
}
}
和第二种方式:
public class MaritalStatusComboBox:ComboBox
{
List<string> list = new List<string>() { "Single", "Married" };
public MaritalStatusComboBox()
{
this.Items.Clear();
foreach (string str in list)
{
this.Items.Add(str);
}
}
}
组合框的输出包括它:单已婚收集为什么收藏夹会出现在我的组合框中?
在第一种方法中,您没有连接数据源:
BindingSource bs = new BindingSource();
bs.DataSource = new List<string> { "Single", "Married" };
this.DataSource = bs;
第二种方法奏效了。 发布的代码未显示"集合"一词。