绑定后,原list值变动,显示内容会刷新
绑定后,list新添加的值时不会显示到界面,需要重新绑定list
微软的Bug
参考代码
cs
public class Student
{
public string Name { get; set; }
}
List<Student> list = new List<Student>();
private void Form2_Load(object sender, EventArgs e)
{
list.Add(new Student() { Name = "张三" });
list.Add(new Student() { Name = "李四" });
this.dataGridView1.DataSource= new BindingList<Student>(list);
}
private void button1_Click(object sender, EventArgs e)
{
//注意 新添加的数据是无法展示到dgv里面的!!!
list.Add(new Student() { Name = "王五" });
list.Add(new Student() { Name = "赵六" });
dataGridView1.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
//元素删除
list.RemoveAt(0);
dataGridView1.Refresh();
}
private void button3_Click(object sender, EventArgs e)
{
//元素修改
list[0].Name=DateTime.Now.ToString();
dataGridView1.Refresh();
}
private void button4_Click(object sender, EventArgs e)
{
//重新绑定
if (list.Count!=dataGridView1.Rows.Count)
{
this.dataGridView1.DataSource=null;
this.dataGridView1.DataSource= new BindingList<Student>(list);
}
else{dataGridView1.Refresh();}
}