实现DataGridView与List双向绑定,List增删查改,DataGridView实时刷新

需要使用 BindingList , BindingList  实现了IRaiseItemChangedEvents 接口,通知客户端属性更改。

并且绑定的Entity 也要实现 INotifyPropertyChanged ,通知客户端实体属性更改

然后dataGridView 就能实现随 Lis实时刷新,WPF 的MVVM 也是依靠INotifyPropertyChanged  实现的。

  

复制代码
public partial class Form2 : Form
    {
        BindingList<Student> students;
        public Form2()
        {
            InitializeComponent();
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.AllowUserToAddRows = false;
            students= new BindingList<Student>();
            students.Add(new Student { Name = "张三", Age = 17 ,SortNum=1});
            students.Add(new Student { Name = "李四", Age = 19,SortNum = 2});
            this.bindingSource1.DataSource = students;
            this.dataGridView1.DataSource =this.students;
        }
        public  class Student: BindableBase
        {
            //public int SortNum { get; set; }
            //public string Name { get; set; }
            //public string Age { get; set; }
            private int sortNum = 0;
            private  string name = string.Empty;
            private int age = 0;
            public string Name { get => name; set => SetProperty(ref name, value); }
            public int Age { get => age; set => SetProperty(ref age, value); }
            public int SortNum { get => sortNum; set => SetProperty(ref sortNum, value); }
        }
        public abstract class BindableBase : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName] string propertyName = null)
            {
                if (!EqualityComparer<T>.Default.Equals(field, newValue))
                {
                    field = newValue;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                    return true;
                }
                return false;
            }
        }
        int no = 1;
        /// <summary>
        /// 新增行,BindingList 实现IRaiseItemChangedEvents,可以实现List增删行,dataGridView1行也实时增删
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            students.Add(new Student { Name="王二麻"+no,Age=18,SortNum=no+2});
            no++;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var first = students.OrderBy(s=>s.SortNum).FirstOrDefault();
        }
        /// <summary>
        /// Student 实现了INotifyPropertyChanged,所以list的Student字段修改,dataGridView1的行数据也更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            var first = students.OrderBy(s => s.SortNum).FirstOrDefault();
            first.Age ++;
        }
    }
复制代码

 

 

posted @   Adam·zhang  阅读(2973)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示