使用BindingList来实现DataGridview数据源为list时的动态增删改
当DataGridview的数据源list的时候,对list进行操作后重新绑定,数据并不会更新
使用BindingList能很好的解决这个问题(framework2.0新增)
例如,使用list时候的代码
/// <summary> /// 性别类型维护 /// </summary> public partial class SexFrm : Form { IList<SystemCode> list; private void BindData() { list =new SystemCodeManager().GetModelByType(type); dgvSexType.DataSource = list; } private void btnAdd_Click(object sender, EventArgs e) { SystemCode sys = new SystemCode(); sys.CodeNo = ""; sys.EnglishName = ""; sys.ID = 12312; sys.Name = ""; sys.QuickSign = ""; sys.Remark = ""; sys.Type = type; list.Add(sys); }} |
这样的情况下,数据源改变,实际显示数据并没有改变
即时你在add的方法里重新绑定数据源
dgvSexType.DataSource = list; |
依然如此. |
在此,使用<span class="Apple-style-span" style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; white-space: normal;">BindingList就可以很好的解决这个问题了.</span> |
/// <summary> /// 性别类型维护 /// </summary> public partial class SexFrm : Form { int type = 2; SystemCodeManager sysManager = new SystemCodeManager(); BindingList<SystemCode> list; private void SexFrm_Load(object sender, EventArgs e) { new BaseCode.LanguageSeting().LoadLanuageSettings(this); BindData(); } private void BindData() { list = new BindingList<SystemCode>(new SystemCodeManager().GetModelByType(type)); dgvSexType.DataSource = list; } private void btnAdd_Click(object sender, EventArgs e) { SystemCode sys = new SystemCode(); sys.CodeNo = ""; sys.EnglishName = ""; sys.ID = 12312; sys.Name = ""; sys.QuickSign = ""; sys.Remark = ""; sys.Type = type; |
list.Add(sys); |
<span class="Apple-style-span"><br></span><span class="Apple-style-span"> }</span> |
} |
<span style="font-family: verdana, Arial, Helvetica, sans-serif;">这样子,就可以轻松的实现和DataGridview互动了</span> |
<span style="font-family: verdana, Arial, Helvetica, sans-serif;">效果如图</span> |
<span style="font-family: verdana, Arial, Helvetica, sans-serif;">点击新增后可以直接新增,如果使用list,就没有任何反应</span> |
|
|

<span style="font-family: verdana, Arial, Helvetica, sans-serif;"><br></span> |
<span style="font-family: verdana, Arial, Helvetica, sans-serif;">注意:BindList需要引用System.ComponentModel命名空间</span> |
面向对象中集合类一般都会实现接口IbindingList, 因为 ,在绑定数据源的时候,如果数据源实现了IbindingList 那么界面可以与之实行互动。无意中发现了微软在2.0增加了一个新类,BindingList<T>,这个类从Collection<T>断承,并实现了IbindingList.
IbindingList 的魅力之处就在于他有AddNew,ApplySort,ListChangedEventHandler 等方法。而BindingSource 是控件的数据源和真正的数据源之间的桥梁,它可以调用IbindingList 的数AddNew等方法。同时IbindingList有数据发生改变的时候又会通知BindingSource从而更新界面。
MS 的 BindingList<T>还不支持sort ,search.,这是因为不知T为何东东有关系,要想实现些功能只有自己扩展了。BindingList<T>有点遗憾的是没有记下删除的数据,这与功能强大的表还是无法相比。从面向对象都已经到面向方面了,怎么在基础类中对面向对象支持还是不太完美呢。现在在研究,对象实体,集合,欢迎各位大师前来指教。
下面是MSDN上对BindingListr 的说明,代码示例演示如何绑定到一个包含业务对象的 BindingList 组件。
http://msdn2.microsoft.com/zh-cn/library/ms132679.aspx#Mtps_DropDownFilterTextBindingList 类可以用作创建双向数据绑定机制的基类。BindingList 提供IBindingList 接口的具体泛型实现。这样就不必实现完整的 IBindingList 接口,实现完整接口可能会因 IBindingList、IEditableObject 和关联的 CurrencyManager 之间微妙的交互而变得比较困难。不过,典型的解决方案程序员将使用提供数据绑定功能的类(如 BindingSource),而不是直接使用 BindingList。
BindingList 通过可扩展的 AddNew 方法支持工厂创建的实例。(在 BindingSource 等其他类中也存在这种类型的扩展性)此外,由于此类实现 ICancelAddNew 接口,因此它通过 EndNew 和 CancelNew 方法实现新项的事务性提交或回滚。
=============================简单案例如下==============
class student//内部类
{
public string xm { get; set; }
public int nl { get; set; }
}
List<student> list = new List<student>();
BindingList<student> list2;
private void button3_Click(object sender, EventArgs e)
{
list = new List<student>(){
new student{xm="张三",nl=23},
new student{xm="李四",nl=23},
new student{xm="王五",nl=23}
};
list2 = new BindingList<student>(list); ;
dataGridView1.DataSource = list2;
}
private void button4_Click(object sender, EventArgs e)
{
list2.Add(new student() { xm="刘德华",nl=11});
}
使用BindingList来实现DataGridview数据源为list时的动态增删改的更多相关文章
- C# - VS2019 通过DataGridView实现对Oracle数据表的增删改查
前言 通过VS2019建立WinFrm应用程序,搭建桌面程序后,通过封装数据库操作OracleHelper类和业务逻辑操作OracleSQL类,进而通过DataGridView实现对Oracle数据表 ...
- WinForm程序用使用List对象绑定DataGridView数据源
1. 在用List<T>对象绑定DataGridView数据源属性的时候,数据源的内容不会动态更新,如果List<T>对象集合中的数据发生变化,那么数据控件的数据源是不会得到更 ...
- DataGridView绑定泛型List时,利用BindingList来实现增删查改
DataGridView绑定泛型List时,利用BindingList来实现增删查改 一. DataGridView绑定泛型List的种种 1.DataGridView数据绑定对比(DataTa ...
- C# DataGridView绑定List对象时,利用BindingList来实现增删查改
当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGridView的数据会随之改变,无需重新绑定到DataGridView. 当Da ...
- 在VS2005编程中,有的时候DataGridView数据源有几个表的联合查询,而系统又有限制为一个表,怎么办?
在VS2005编程中,有的时候DataGridView数据源有几个表的联合查询,而系统又有限制为一个表,怎么办? 解决方法:在SqlServer的企业管理器里增加一个视图吧!!!!!!!!(从来没用过 ...
- 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性
基于.net的分布式系统限流组件 在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...
- winform窗体(六)——DataGridView控件及通过此控件中实现增删改查
DataGridView:显示数据表,通过此控件中可以实现连接数据库,实现数据的增删改查 一.后台数据绑定: List<xxx> list = new List<xxx> ...
- WinForm DataGridView增删改查
DataGridView连接数据库对表进行增删改查 一.绑定数据源 //做一个变量控制页面刷新 ; public Form1() { InitializeComponent(); } private ...
- 使用DataGridView进行增删改查,并同步到数据库
DataGridView控件具有极高的可配置性和可扩展性.它提供有大量的属性.方法和事件,能够用来对该控件的外观和行为进行自己定义.以下通过一个小样例来展示DataGridView进行增删改查,并同步 ...
随机推荐
- Visual Basic for Application
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The note of Visual Basic for Applicati ...
- Objective-C类成员变量深度剖析--oc对象内存模型
目录 Non Fragile ivars 为什么Non Fragile ivars很关键 如何寻址类成员变量 真正的“如何寻址类成员变量” Non Fragile ivars布局调整 为什么Objec ...
- Challenge–response authentication 挑战(询问)应答机制
In computer security, challenge–response authentication is a family of protocols in which one party ...
- vue-router同路由地址切换无效解决
本来还想写的,一搜就有现成的,算了: http://blog.csdn.net/peng_guan/article/details/59702699
- react新版本生命周期
给componentWillMount componentWillReceiveProps componentWillUpdate生命周期加上UNSAFE_前缀,表明其不安全性,并将在未来版本将其移除 ...
- python学习笔记之小小购物车
#coding=utf-8 ''' Created on 2015-6-18 @author: 悦文 ''' def goods_list(): shangpin={"} print &qu ...
- jQuery默认select选择第一个元素
$("#id option:first").prop("selected", 'selected');
- eas之网络互斥功能示手工控制
public void doMutexService() { IMutexServiceControl mutex = MutexServiceControlFactory.get ...
- android keystore的生成和使用
android要求所有的程序必须有签名,否则就不会安装该程序.在我们开发过程中,adt使用debug keystore,在 preference->android->buid中设置.deb ...
- css实现面包屑导航
HTML代码: <div id="breadcrumb"> <ul class="crumbs"> <li class=" ...