欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

WinForm DataGridView双向数据绑定

程序员文章站 2022-09-04 18:34:52
程序目标: 实现DataGridView与BindingList双向绑定。用户通过DataGridView修改值后立即更新BindList对象的值,代码修改BindList后立即更新DataGridView的显示。 实现环境:vs2017 C# WinForm Code: 1 ///***** ......

程序目标:

  实现datagridview与bindinglist<t>双向绑定。用户通过datagridview修改值后立即更新bindlist对象的值,代码修改bindlist后立即更新datagridview的显示。

实现环境:vs2017 c# winform

code:

WinForm DataGridView双向数据绑定
  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
WinForm DataGridView双向数据绑定
 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