当前位置:   article > 正文

dataGridView 绑定List 显示内容不刷新

dataGridView 绑定List 显示内容不刷新

绑定后,原list值变动,显示内容会刷新

绑定后,list新添加的值时不会显示到界面,需要重新绑定list

微软的Bug

参考代码

  1. public class Student
  2. {
  3. public string Name { get; set; }
  4. }
  5. List<Student> list = new List<Student>();
  6. private void Form2_Load(object sender, EventArgs e)
  7. {
  8. list.Add(new Student() { Name = "张三" });
  9. list.Add(new Student() { Name = "李四" });
  10. this.dataGridView1.DataSource= new BindingList<Student>(list);
  11. }
  12. private void button1_Click(object sender, EventArgs e)
  13. {
  14. //注意 新添加的数据是无法展示到dgv里面的!!!
  15. list.Add(new Student() { Name = "王五" });
  16. list.Add(new Student() { Name = "赵六" });
  17. dataGridView1.Refresh();
  18. }
  19. private void button2_Click(object sender, EventArgs e)
  20. {
  21. //元素删除
  22. list.RemoveAt(0);
  23. dataGridView1.Refresh();
  24. }
  25. private void button3_Click(object sender, EventArgs e)
  26. {
  27. //元素修改
  28. list[0].Name=DateTime.Now.ToString();
  29. dataGridView1.Refresh();
  30. }
  31. private void button4_Click(object sender, EventArgs e)
  32. {
  33. //重新绑定
  34. if (list.Count!=dataGridView1.Rows.Count)
  35. {
  36. this.dataGridView1.DataSource=null;
  37. this.dataGridView1.DataSource= new BindingList<Student>(list);
  38. }
  39. else{dataGridView1.Refresh();}
  40. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/306284?site
推荐阅读
相关标签
  

闽ICP备14008679号