WinForm—ComboBox控件数据绑定

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/csdn_wuwt/article/details/84065424

WinForm—ComboBox控件数据绑定
1、List< string >

IList<string> list = new List<string>();
list.Add("上");
list.Add("下");
list.Add("左");
list.Add("右");
comboBox1.DataSource = list;

2、List< Info >

public class Info
{
    public string Id { get; set; }
    public string Name { get; set; }		
}

IList<Info> infoList = new List<Info>();
Info info1 = new Info() { Id="1",Name="上"};
Info info2 = new Info() { Id="2",Name="下"};
Info info3 = new Info() { Id = "3",Name = "左" };
 Info info3 = new Info() { Id = "4",Name = "右" };
infoList.Add(info1);
infoList.Add(info2);
infoList.Add(info3);
infoList.Add(info4);
comboBox1.DataSource = infoList;
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";

3、Dictionary<int , string>

Dictionary<int, string> kvDictonary = new Dictionary<int, string>();
kvDictonary.Add(1, "上");
kvDictonary.Add(2, "下");
kvDictonary.Add(3, "左");
kvDictonary.Add(4, "右");

BindingSource bs = new BindingSource();
bs.DataSource = kvDictonary;
comboBox1.DataSource = bs;
comboBox1.ValueMember = "Key";
comboBox1.DisplayMember = "Value";

猜你喜欢

转载自blog.csdn.net/csdn_wuwt/article/details/84065424