WinForm—ComboBox控制元件資料繫結
阿新 • • 發佈:2018-11-15
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";