我有100个具有id和名称的区域对象我想在组合框中显示名称,并且它们的id是组合框的索引但是这段代码不起作用
private void Areadropdown()//show Areadropdown
{
List<Area> list = Area.Dropdown();
foreach (Area a in list)
{
metroComboBox1.Items.Insert(a.Id,a.Areaname);
}
}
答案 0 :(得分:0)
List<Area> list = new List<Area>();
metroComboBox1.DisplayMember = "PropertyName" //for you want to display in dropdown so give that class property name
metroComboBox1.ValueMember = "ProprtyName" // use for take value of those selection
metroComboBox1.DataSource = list; //your data list
this code help to solve your problem
FYI 请参考以下示例
districtComboBox.DisplayMember = "Name";
districtComboBox.ValueMember = "Id";
districtComboBox.DataSource = districts.Where(d => d.StateId == (int)stateComboBox.SelectedValue).ToList()
答案 1 :(得分:0)
试试这个
使用类中定义的属性名称作为显示成员和成员值
districtComboBox.DisplayMember = "Areaname";
districtComboBox.ValueMember = "Id";
答案 2 :(得分:0)
试试这个(使用BindingList):
private void Areadropdown()
{
List<Area> list = Area.Dropdown();
BindingList<Area> areas = new BindingList<Area>(list);
metroComboBox1.ValueMember = "Id";
metroComboBox1.DisplayMember = "Areaname";
metroComboBox1.DataSource = areas ;
}