C# ComboBox绑定值问题
使用这种方式始终绑定值有问题:
cbxschool.datasource = schoollist;
cbxschool.displaymember = "school_name";
cbxschool.valuemember = "school_id";
选择改变事件获取选中值:cbxschool.selectedvalue 始终是对象,不是想要的id。
解决方法:
if (schoollist != null && schoollist.count > 0)
{
cbxschool.items.clear();
for (int i = 0; i < schoollist.count; i++)
{
cbxschool.items.add(schoollist[i].school_name);
}
//选择默认值
int selectindex = schoollist.findindex(a => a.school_id == schoolid);
cbxschool.selectedindex = selectindex == -1 ? 0 : selectindex;
//获取选中值
string schoolname = schoollist[cbxschool.selectedindex].school_name;
}
上一篇: 小外甥很想要一个小妹妹
下一篇: 表白的秘诀