程序目标:

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

实现环境:vs2017 C# WinForm

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

主要代码:

 ///****************************************************************************
/// CLR版本 :4.0.30319.42000
/// 邮 箱 :282780854@qq.com
/// 博 客 :https://www.cnblogs.com/it89/
/// 创 建 者 :龙腾虎跃
/// 创建日期 :2019/1/15 21:02:04
/// 功能描述 :
/// 使用说明 :
///****************************************************************************
using System;
using System.ComponentModel;
using System.Windows.Forms; namespace TestDataGridViewBind
{
public partial class Form1 : Form
{
private DataGridView mDataGridView;
//private BindingSource mBindingSource; //绑定方式一需要的。
private Button mAddItemBtn;
private Button mChangeItemValueBtn;
private Button mDeleteItemBtn; public BindingList<People> Peoples { get; set; } public Form1()
{
this.Load += this.Form1_Load;
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
//初始化mDataGridView对象
mDataGridView = new DataGridView();
mDataGridView.AutoSize = true;
mDataGridView.Left = ;
mDataGridView.Top = ; //初始化mAddItemBtn按钮。
this.mAddItemBtn = new Button();
mAddItemBtn.Text = "Add People";
mAddItemBtn.AutoSize = true;
mAddItemBtn.Left = ;
mAddItemBtn.Top = ;
mAddItemBtn.Click += this.mAddItemBtn_Click; //初始化mDeleteItemBtn按钮
mDeleteItemBtn = new Button();
mDeleteItemBtn.Text = "Delete Item";
mDeleteItemBtn.AutoSize = true;
mDeleteItemBtn.Left = ;
mDeleteItemBtn.Top = ;
mDeleteItemBtn.Click += mDeleteItemBtn_Click; //初始化mChangeItemValueBtn按钮。
mChangeItemValueBtn = new Button();
mChangeItemValueBtn.Text = "Change Item Value";
mChangeItemValueBtn.AutoSize = true;
mChangeItemValueBtn.Left = ;
mChangeItemValueBtn.Top = ;
mChangeItemValueBtn.Click += this.mChangeItemValueBtn_Click; //初始化Form1。
this.Controls.Add(mDataGridView);
this.Controls.Add(mAddItemBtn);
this.Controls.Add(mDeleteItemBtn);
this.Controls.Add(mChangeItemValueBtn);
this.AutoSize = true;
this.Text = "DataGridView object binding demo"; //初始化Peoples对象。
Peoples = new BindingList<People>();
Peoples.Add(new People("张三", "北京", ));
Peoples.Add(new People("李四", "上海", ));
Peoples.Add(new People("王五", "深圳", )); //绑定方式一:通过BindingSource对象把Peoples绑定到mDataGridView控件。
//mBindingSource = new BindingSource();
//mBindingSource.DataSource = Peoples;
//mDataGridView.DataSource = mBindingSource; //绑定方式二:直接通过mDataGridView.DataBindings绑定Peoples。Peoples不能引发改变通知事件,但是People类型继承了INotifyPropertyChanged接口,可以引发改变通知事件。
mDataGridView.DataBindings.Add("DataSource", this, "Peoples", false, DataSourceUpdateMode.OnPropertyChanged);
} private void mAddItemBtn_Click(object sender, EventArgs e)
{
this.Peoples.Add(new People("新人", "湖南", ));
} private void mDeleteItemBtn_Click(object sender, EventArgs e)
{
if (this.Peoples.Count > )
{
this.Peoples.RemoveAt();
}
} private void mChangeItemValueBtn_Click(object sender, EventArgs e)
{
if (Peoples.Count > )
{
this.Peoples[].Address = "浙江";
//如果People没有继承INotifyPropertyChanged接口,则需要下面注释的代码,来引发改变通知事件。
//this.Peoples.ResetItem(0);//引发改变通知
} if (Peoples.Count > )
{
this.Peoples[].Age = Peoples[].Age + ;
//this.Peoples.ResetItem(1);//引发改变通知
}
}
}
}

Form1.cs

 ///****************************************************************************
/// CLR版本 :4.0.30319.42000
/// 邮 箱 :282780854@qq.com
/// 博 客 :https://www.cnblogs.com/it89/
/// 创 建 者 :龙腾虎跃
/// 创建日期 :2019/1/15 21:03:04
/// 功能描述 :
/// 使用说明 :
///****************************************************************************
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices; namespace TestDataGridViewBind
{
/// <summary>
///
/// </summary>
public class People : INotifyPropertyChanged
{
#region "Public Section"
public string Name
{
get => mName;
set { mName = value; NotifyPropertyChanged("Name"); }
} public string Address
{
get => mAddresss;
set { mAddresss = value; NotifyPropertyChanged("Address"); }
} public int Age
{
get => mAge;
set { mAge = value; NotifyPropertyChanged("Age"); }
} public People(string name, string address, int age)
{
mName = name;
mAddresss = address;
mAge = age;
} public event PropertyChangedEventHandler PropertyChanged; #endregion #region "Private Section"
private string mName;
private string mAddresss;
private int mAge; /// <summary>
/// 该方法由每个属性Set访问器调用。
/// </summary>
/// <param name="propertyName"></param>
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} #endregion
}
}

People.cs

05-06 07:41
查看更多