程序目标:

  实现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. 【转】mysql 中int类型字段unsigned和signed的区别

    转自https://www.cnblogs.com/wangzhongqiu/p/6424827.html 用法: mysql> CREATE TABLE t ( a INT UNSIGNED, ...

  2. Java公开课-06.单例

    一. 什么是单例模式 因程序需要,有时我们只需要某个类同时保留一个对象,不希望有更多对象,此时,我们则应考虑单例模式的设计. 二. 单例模式的特点 1. 单例模式只能有一个实例. 2. 单例类必须创建 ...

  3. 如何发布jar包到maven中央仓库

    自使用maven以来,没少使用maven中央仓库中的各种jar包,方便有效,但是咱们也不能总是只取不予,也应该懂得奉献,当你写好了一个十分好用的jar包,想贡献出去给大家使用的时候,应该怎么做呢?当然 ...

  4. JavaScript Math 对象的常用方法

    JavaScript Math 对象 Math 对象 Math 对象用于执行数学任务. 使用 Math 的属性和方法的语法: var pi_value=Math.PI; var sqrt_value= ...

  5. 新浪微博注册(elenium Python 自动化)

    from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom time import sleep ...

  6. testng生成自定义html报告

    转自:https://blog.csdn.net/kdslkd/article/details/51198433 testng原生的或reportng的报告总有些不符合需要,尝试生成自定义测试报告,用 ...

  7. 一个C++程序中有多个cin输入的情况

    在很多C++程序中,我们都会有几次输入的操作.这种情况下,会遇到只有第一个cin语句被执行了,而后面的cin都没有执行.这是因为cin所输入的数据都放在cin缓冲区中,当第一次使用cin后,缓冲区中已 ...

  8. [ZLXOI2015]殉国 数论 扩展欧几里得

    题目大意:已知a,b,c,求满足ax+by=c (x>=0,y>=0)的(x+y)最大值与最小值与解的个数. 直接exgcd,求出x,y分别为最小正整数的解,然后一算就出来啦 #inclu ...

  9. Angularjs interceptor

    angularJs 请求过滤 新建一个服务, $HttpProvider 中有一个 interceptore 数组,所谓的拦截器就是一个注册到该数组的工厂,该工厂在app.config() 中注入, ...

  10. 列表(list) ----python

    Python 列表(List) 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型 ...