實現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 ++;
        }
    }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM