dataGridView 绑定List 显示内容不刷新

绑定后,原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();}
        }
相关推荐
七七墨染1 天前
DotMemory系列:5. 如何实现自动化抓取和应用自托管
运维·c#·自动化
王家羽翼-王羽1 天前
C# 连接 PLC 的S7西门子协议读写,样例分享
c#
斯内科1 天前
C#进行CAN【控制器局域网】通讯
c#·can·pcanbasic.net
张人玉1 天前
C#WPF——MVVM框架编写管理系统所遇到的问题
开发语言·c#·wpf·mvvm框架
马达加斯加D1 天前
C# --- 如何写UT
前端·c#·log4j
Charles_go1 天前
C#中级39、什么是依赖注入设计模式
java·设计模式·c#
eggcode1 天前
C#开源库ACadSharp将Dwg转Dxf
c#·dxf·dwg
拼好饭和她皆失1 天前
C#学习入门
开发语言·学习·c#
小小编程能手1 天前
大小端字节序
c#
冒泡P1 天前
【Unity】TextMeshPro富文本中使用精灵图集
ui·unity·c#·游戏引擎