【WinForm】下拉框ComboBox绑定数据

下拉框绑定数据

绑定list实体集

直接绑定

cmb.DataSource = list;
cmb.ValueMember = "ID";
cmb.DisplayMember = "Name";

也可以通过BindingSource来绑定

cmb.DataSource = new BindingSource() { DataSource = list };
cmb.ValueMember = "ID";
cmb.DisplayMember = "Name";

 

ValueMember是实际的值

DisplayMember是要显示的值

可以通过cmb.SelectedValue获取选中的值

 

绑定字典

绑定字典比较特殊,只能用BindingSource绑定

Dictionary<int, string> dic = new Dictionary<int, string>()
{
    { 1,"星期日"},
    { 2,"星期一"},
    { 3,"星期二"},
    { 4,"星期三"},
    { 5,"星期四"},
    { 6,"星期五"},
    { 7,"星期六"},
};
cmb.DataSource = new BindingSource() { DataSource = dic };
cmb.ValueMember = "Key";
cmb.DisplayMember = "Value";

 

 

 

相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页