程序目标:

  实现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

WinForm DataGridView双向数据绑定的更多相关文章

  1. C#实现WinForm DataGridView控件支持叠加数据绑定

    我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支 ...

  2. [Winform] DataGridView 总结(FAQ)

    Q1.  如何使单元格不可编辑? A:设置 ReadOnly 属性,可以设置的对象包括 DataGridViewRow(行).DataGridViewColumn(列).DataGridViewCel ...

  3. 关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案

    上周在开发Winform 项目中,我曾遇到一个看似简单,但一直都没有解决的问题,那就是:设置winform DataGridView控件的行DefaultCellStyle,但却没有任何变化,我也曾求 ...

  4. vue双向数据绑定原理探究(附demo)

    昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...

  5. 双向数据绑定(angular,vue)

    最近github上插件项目更新了关于双向数据绑定的实现方式,关于angular和vue. angular众所周知是使用的脏检查($dirty).一开始大家会认为angular开启了类似setInter ...

  6. jQuery.my – 实时的复杂的双向数据绑定

    jQuery.my 这个插件用于实时双向数据绑定.它发生变异给出的数据源对象,反映了用户与用户界面之间的相互作用.jQuery.my 提供了全面的验证,条件格式,复杂的依赖关系,运行形式结构操作. 马 ...

  7. Angular双向数据绑定MVVM以及基本模式分析

    MVVM: angular的MVVM实现的是双向数据绑定,模型从服务器端抓取到数据,将数据通过控制器(controller)传递到视图(view)显示,视图数据发生变化时同样也会影响到模型数据的变化, ...

  8. 《AngularJS权威教程》中关于指令双向数据绑定的理解

    在<AngularJS权威教程>中,自定义指令和DOM双向数据绑定有一个在线demo,网址:http://jsbin.com/IteNita/1/edit?html,js,output,具 ...

  9. Angular解决双向数据绑定

    <!DOCTYPE html> <html ng-app="myApp1"><body><div ng-controller=" ...

随机推荐

  1. linux下安装nginx与配置

    linux系统为Centos 64位 第一步:从http://nginx.org/download/上下载相应的版本(或者wget http://nginx.org/download/nginx-1. ...

  2. 使用SimHash进行海量文本去重[转载]

    阅读目录 1. SimHash与传统hash函数的区别 2. SimHash算法思想 3. SimHash流程实现 4. SimHash签名距离计算 5. SimHash存储和索引 6. SimHas ...

  3. ps命令手册

    LINFO ps命令   在PS(即,处理状态)命令被用来提供关于当前正在运行的信息的过程,包括它们的过程标识号(PID)来. 过程也称为任务,是程序的执行(即运行)实例.系统为每个过程分配一个唯一的 ...

  4. Nagios安装、配置、问题记录

    http://youyizhimen.blog.163.com/blog/static/170917267201201745523276/ 本文描述了我在使用Nagios的过程中遇到的一些问题.解决办 ...

  5. 27.app后端搭建聊天服务器的经历

    现在,聊天功能已经成了社交app的标配了.但是,众多web开发出生的程序员对聊天相关的服务的不了解,带来了很多开发上的困扰.在这篇文章中,根据下面3个方面,谈谈聊天服务. 1.      聊天服务的技 ...

  6. 关于ftp用户连接时出现500 OOPS: cannot change directory的解决办法

    RHEL5 中配置好后,今天想在XP下用ftp连接虚拟机中的linux,但ftp连接的时候会出现 "500 OOPS:cannot change directory:/root" ...

  7. Java 学习笔记 (三) Java 日期类型

    以下内容摘自:  https://www.cnblogs.com/crazylqy/p/4172324.html import java.sql.Timestamp; import java.text ...

  8. admin-handlers.go

    package],,) ],,) ],,) ],,) ],,) ])     if err == redis.Nil {         http.NotFound(w, r)     } else ...

  9. mysql千万级大数据SQL查询优化

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  10. Android性能优化-内存泄漏的8个Case

    1为什么要做性能优化? 手机性能越来越好,不用纠结这些细微的性能? Android每一个应用都是运行的独立的Dalivk虚拟机,根据不同的手机分配的可用内存可能只有(32M.64M等),所谓的4GB. ...