程序目标:

  实现DataGridView与BindingList<T>双向绑定。用户通过DataGridView修改值后立即更新BindList对象的值,代码修改BindList后立即更新DataGridView的显示。

实现环境:vs2017 C# WinForm

程序完整代码包:https://pan.baidu.com/s/1LLUxL1UyqNWkXkPF_LuEig

主要代码:

  1 ///****************************************************************************
  2 /// CLR版本     :4.0.30319.42000
  3 /// 邮    箱    :282780854@qq.com
  4 /// 博    客    :https://www.cnblogs.com/it89/
  5 /// 创 建 者    :龙腾虎跃
  6 /// 创建日期    :2019/1/15 21:02:04 
  7 /// 功能描述    :
  8 /// 使用说明    :
  9 ///****************************************************************************
 10 using System;
 11 using System.ComponentModel;
 12 using System.Windows.Forms;
 13 
 14 namespace TestDataGridViewBind
 15 {
 16     public partial class Form1 : Form
 17     {
 18         private DataGridView mDataGridView;
 19         //private BindingSource mBindingSource; //绑定方式一需要的。
 20         private Button mAddItemBtn;
 21         private Button mChangeItemValueBtn;
 22         private Button mDeleteItemBtn;
 23 
 24         public BindingList<People> Peoples { get; set; }
 25 
 26         public Form1()
 27         {
 28             this.Load += this.Form1_Load;
 29             InitializeComponent();
 30         }
 31         public void Form1_Load(object sender, EventArgs e)
 32         {
 33             //初始化mDataGridView对象
 34             mDataGridView = new DataGridView();
 35             mDataGridView.AutoSize = true;
 36             mDataGridView.Left = 10;
 37             mDataGridView.Top = 40;
 38 
 39             //初始化mAddItemBtn按钮。
 40             this.mAddItemBtn = new Button();
 41             mAddItemBtn.Text = "Add People";
 42             mAddItemBtn.AutoSize = true;
 43             mAddItemBtn.Left = 30;
 44             mAddItemBtn.Top = 10;
 45             mAddItemBtn.Click += this.mAddItemBtn_Click;
 46 
 47             //初始化mDeleteItemBtn按钮
 48             mDeleteItemBtn = new Button();
 49             mDeleteItemBtn.Text = "Delete Item";
 50             mDeleteItemBtn.AutoSize = true;
 51             mDeleteItemBtn.Left = 120;
 52             mDeleteItemBtn.Top = 10;
 53             mDeleteItemBtn.Click += mDeleteItemBtn_Click;
 54 
 55             //初始化mChangeItemValueBtn按钮。
 56             mChangeItemValueBtn = new Button();
 57             mChangeItemValueBtn.Text = "Change Item Value";
 58             mChangeItemValueBtn.AutoSize = true;
 59             mChangeItemValueBtn.Left = 220;
 60             mChangeItemValueBtn.Top = 10;
 61             mChangeItemValueBtn.Click += this.mChangeItemValueBtn_Click;
 62 
 63             //初始化Form1。
 64             this.Controls.Add(mDataGridView);
 65             this.Controls.Add(mAddItemBtn);
 66             this.Controls.Add(mDeleteItemBtn);
 67             this.Controls.Add(mChangeItemValueBtn);
 68             this.AutoSize = true;
 69             this.Text = "DataGridView object binding demo";
 70 
 71             //初始化Peoples对象。
 72             Peoples = new BindingList<People>();
 73             Peoples.Add(new People("张三", "北京", 25));
 74             Peoples.Add(new People("李四", "上海", 28));
 75             Peoples.Add(new People("王五", "深圳", 30));
 76 
 77             //绑定方式一:通过BindingSource对象把Peoples绑定到mDataGridView控件。
 78             //mBindingSource = new BindingSource();
 79             //mBindingSource.DataSource = Peoples;
 80             //mDataGridView.DataSource = mBindingSource;
 81 
 82             //绑定方式二:直接通过mDataGridView.DataBindings绑定Peoples。Peoples不能引发改变通知事件,但是People类型继承了INotifyPropertyChanged接口,可以引发改变通知事件。
 83             mDataGridView.DataBindings.Add("DataSource", this, "Peoples", false, DataSourceUpdateMode.OnPropertyChanged);
 84         }
 85 
 86         private void mAddItemBtn_Click(object sender, EventArgs e)
 87         {
 88             this.Peoples.Add(new People("新人", "湖南", 30));
 89         }
 90 
 91         private void mDeleteItemBtn_Click(object sender, EventArgs e)
 92         {
 93             if (this.Peoples.Count > 0)
 94             {
 95                 this.Peoples.RemoveAt(0);
 96             }
 97         }
 98 
 99         private void mChangeItemValueBtn_Click(object sender, EventArgs e)
100         {
101             if (Peoples.Count > 0)
102             {
103                 this.Peoples[0].Address = "浙江";
104                 //如果People没有继承INotifyPropertyChanged接口,则需要下面注释的代码,来引发改变通知事件。
105                 //this.Peoples.ResetItem(0);//引发改变通知
106             }
107 
108             if (Peoples.Count > 1)
109             {
110                 this.Peoples[1].Age = Peoples[1].Age + 1;
111                 //this.Peoples.ResetItem(1);//引发改变通知
112             }
113         }
114     }
115 }
Form1.cs
 1 ///****************************************************************************
 2 /// CLR版本     :4.0.30319.42000
 3 /// 邮    箱    :282780854@qq.com
 4 /// 博    客    :https://www.cnblogs.com/it89/
 5 /// 创 建 者    :龙腾虎跃
 6 /// 创建日期    :2019/1/15 21:03:04 
 7 /// 功能描述    :
 8 /// 使用说明    :
 9 ///****************************************************************************
10 using System;
11 using System.ComponentModel;
12 using System.Runtime.CompilerServices;
13 
14 namespace TestDataGridViewBind
15 {
16     /// <summary>
17     /// 
18     /// </summary>
19     public class People : INotifyPropertyChanged
20     {
21         #region "Public Section"
22         public string Name
23         {
24             get => mName;
25             set { mName = value; NotifyPropertyChanged("Name"); }
26         }
27 
28         public string Address
29         {
30             get => mAddresss;
31             set { mAddresss = value; NotifyPropertyChanged("Address"); }
32         }
33 
34         public int Age
35         {
36             get => mAge;
37             set { mAge = value; NotifyPropertyChanged("Age"); }
38         }
39 
40         public People(string name, string address, int age)
41         {
42             mName = name;
43             mAddresss = address;
44             mAge = age;
45         }
46 
47         public event PropertyChangedEventHandler PropertyChanged;
48 
49         #endregion
50 
51         #region "Private Section"
52         private string mName;
53         private string mAddresss;
54         private int mAge;
55 
56         /// <summary>
57         /// 该方法由每个属性Set访问器调用。
58         /// </summary>
59         /// <param name="propertyName"></param>
60         private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
61         {
62             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
63         }
64 
65         #endregion
66     }
67 }
People.cs
posted on 2019-01-15 23:25  IT菜鸟的博客  阅读(3262)  评论(0编辑  收藏  举报