程式目標: 實現DataGridView與BindingList<T>雙向綁定。用戶通過DataGridView修改值後立即更新BindList對象的值,代碼修改BindList後立即更新DataGridView的顯示。 實現環境:vs2017 C# WinForm Code: 1 ///***** ...
程式目標:
實現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 /// 郵 箱 :[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