因为ListBox的数据源可以是DataTable或List,但Dictionary却不能直接作为它的数据源。但我们可以利用BindingSource来作为连接数据源与控件的桥梁。
实现如下:
Dictionary<int,string> userList=new Dictionary<int,string>();
userList.Add(101,”李明”);
userList.Add(102,”刘红”);
userList.Add(103,”张三”);
//绑定数据
comboBox1.DataSource = new BindingSource(dict, null);
comboBox1.ValueMember = “Key”;//文本对应的值
comboBox1.DisplayMember = “Value”;//显示的文本
利用BindingSource将Dictionary绑定到ListBox:实例解析
本文介绍了如何通过BindingSource将Dictionary类型的用户列表映射到ListBox控件,展示了如何设置DataSource、ValueMember和.DisplayMember。
8420

被折叠的 条评论
为什么被折叠?



