WinForm DataGridView双向数据绑定

Posted it89

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WinForm DataGridView双向数据绑定相关的知识,希望对你有一定的参考价值。

程序目标:

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

实现环境:vs2017 C# WinForm

Code:

技术分享图片
  1 ///****************************************************************************
  2 /// CLR版本     :4.0.30319.42000
  3 /// 邮    箱    :[email protected]
  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 /// 邮    箱    :[email protected]
 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

以上是关于WinForm DataGridView双向数据绑定的主要内容,如果未能解决你的问题,请参考以下文章

C# winform DataGridView 如何实现删除 更新 手动绑定数据源(不是在控件里指定数据源) 求大神 在线等

datagridview 点击列标题排序

用dataGridView将数据绑上去后,再修改其中的数据,sql语句测试正确,却改不了数据,为啥呢?

c# winform DataGridView添加一行,添加数据后,保存到数据库

c# winform datagridview界面上的行删了,但datagridview数据源没有同步如何解决

C# winform datagridview如何计算统计