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();}
        }
相关推荐
我是唐青枫2 小时前
深入理解 C#.NET Parallel:并行编程的正确打开方式
开发语言·c#·.net
yue0082 小时前
C# ASCII和字符串相互转换
c#
TypingLearn2 小时前
Perigon.CLI 10.0 重磅发布【AspNetCore开发模板和辅助工具】
c#·.net·aspnetcore
Sheep Shaun3 小时前
STL中的map和set:红黑树的优雅应用
开发语言·数据结构·c++·后端·c#
kylezhao20196 小时前
C# 中常用的定时器详解
开发语言·c#
秋雨雁南飞6 小时前
C# 动态脚本执行器
c#·动态编译
月巴月巴白勺合鸟月半7 小时前
用AI生成一个简单的视频剪辑工具 的后续
c#
钰fly7 小时前
Windows Forms开发工具与功能总结表
前端·c#
lzhdim8 小时前
C#性能优化:从入门到入土!这10个隐藏技巧让你的代码快如闪电
开发语言·性能优化·c#
=PNZ=BeijingL8 小时前
SprintBoot +Screw+PostgreSQL生成数据库文档时空指针问题
开发语言·c#