选择使用字典来保存数据,并加载到窗台控件中,但是按照List<T>的方式直接绑定,数据并不显示,解决方法:
这里使用了一个SE类
/// <summary>
/// 程序员类
/// </summary>
public class SE
{
public string ID { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 性别
/// </summary>
public Gender Gender { get; set; }
}
}
在窗台Form1中加载数据
public partial class Form1 : Form { Dictionary<string, SE> dic = new Dictionary<string, SE>(); public Form1() { InitializeComponent(); } void BindData() { SE se1 = new SE(); se1.ID = "100001"; se1.Name = "张三"; SE se2 = new SE(); se2.ID = "100002"; se2.Name = "李四"; dic.Add(se1.ID,se1); dic.Add(se2.ID,se2); BindingSource bs = new BindingSource(); bs.DataSource = dic.Values; dataGridView1.DataSource = bs; } private void Form1_Load(object sender, EventArgs e) { BindData(); } }