DataGridView绑定泛型List时,利用BindingList来实现增删查改
DataGridView绑定泛型List时,利用BindingList来实现增删查改
一、 DataGridView绑定泛型List的种种
1、DataGridView数据绑定对比(DataTable与泛型List):
当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGridView的数据会随之改变,无需重新绑定到DataGridView。
当DataGridView的DataSource是泛型List,当List的数据改变时,则需要先将DataGridView的DataSource设置为new List<T>(),再将改变后的List<T>赋给DataGridView的DataSource。
绑定List时,注意:切莫将DataGridView的DataSource设置为Null,否则会破坏DataGridView的列结构。
2、数据绑定后的添加删除问题:
如果要对绑定在DataGridView中的List<T>进行数据的添加删除,先要把List<T>转换成BindingList<T>,再进行绑定:DataGridView.DataSource=new BindingList<T>(new List<T>)。否则的话会产生许多意想不到的错误。
如:初始绑定空数据后再添加数据绑定后,却取不到DataGridView.CurrentCell属性。
3、使用泛型绑定依然可以添加删除:
IList<T> list= new List<T>();
DataGridView.DataSource=list;//DataGridView的行不能添加删除
DataGridView.DataSource=new BindingList<T>(list);//DataGridView的行可以添加删除(只有允许添加行、删除行)
二、 DataGridView绑定泛型List时,利用BindingList来实现增删查改
此处以学生的信息为例,演示界面如下:

项目目录如下:

Student这个model类里面的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DGVBandingList
{
class Student
{
public string StudentID { get; set; }
public string StudentName { get; set; }
public string Telephone { get; set; }
public string QQ { get; set;}
}
}
对Form1操作的代码(含注释)如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DGVBandingList
{
public partial class Form1 : Form
{
//定义一个IList的学生链表
private IList<Student> IStudent = new List<Student>();
//定义一个BindingList的学生链表
private BindingList<Student> BStudent;
public Form1()
{
InitializeComponent();
//假设此为List的初始数据
initialData();
//将IList中的值赋给对应的BindingList
BStudent = new BindingList<Student>(IStudent);
//将DataGridView里的数据源绑定成BindingList
this.dgvStudentInfo.DataSource = BStudent;
}
private IList<Student> initialData()
{
for (int i = 0; i < 3; i++)
{
Student initialSt = new Student();
initialSt.StudentID = "ID" + i;
initialSt.StudentName = "name" + i;
initialSt.Telephone = "tel" + i;
initialSt.QQ = "QQ" + i;
IStudent.Add(initialSt);
}
return IStudent;
}
private void btnAdd_Click(object sender, EventArgs e)
{
Student studentTemp = new Student();
studentTemp.StudentID = "S1001";
studentTemp.StudentName = "张三";
studentTemp.Telephone = "123456789";
studentTemp.QQ = "45621578";
//BindingList的数据改变时,DataGridView的数据会随之改变,无需重新绑定到DataGridView
BStudent.Add(studentTemp); //默认添加到DGV的最后一行
}
private void btnDelete_Click(object sender, EventArgs e)
{
//允许删除多行
DataGridViewSelectedRowCollection rows = this.dgvStudentInfo.SelectedRows;
foreach (DataGridViewRow row in rows)
{
this.BStudent.RemoveAt(row.Index);
}
//若想每次只能删除一行
//得到当前选中行的索引
//int index = this.dgvStudentInfo.CurrentRow.Index;
//根据索引,删除DataGridView里面选中的记录
//this.BStudent.RemoveAt(index);
}
//使用insert的时候首先要在界面上选择一行,表示插入这行之后
//否则默认插入第一行之后,因为第一行是默认被选中的。
private void btnInsert_Click(object sender, EventArgs e)
{
int index = this.dgvStudentInfo.SelectedRows[0].Index + 1;
Student insertStudent = new Student();
insertStudent.StudentID = "S2001";
insertStudent.StudentName = "王五";
insertStudent.Telephone = "4522166655";
insertStudent.QQ = "895545512";
BStudent.Insert(index, insertStudent);
}
private void btnModify_Click(object sender, EventArgs e)
{
//因为DGV里是整行选择,故先将所选行装配成一个model
Student modifySt = this.dgvStudentInfo.CurrentRow.DataBoundItem as Student;
//得到当前选中行的索引
int index = this.dgvStudentInfo.CurrentRow.Index;
//做出修改,如修改电话号码为aaaa
modifySt.Telephone = "aaa";
//删除当前行
this.BStudent.RemoveAt(index);
//添加修改后的行
this.BStudent.Insert(index, modifySt);
}
}
}
界面演示的结果:
运行初始:

点击“添加”按钮后:

点击“删除”按钮,若选择删除第二行:

点击“插入”,选择插在name2下面:

点击“修改”,修改最后一行,即张三:

最后,值得注意的是:
以上的“添加”,“删除”,“插入”,“修改”操作只是对BindingList做了改变,反映到了DataGridView里面,但并没有去变动List中的值,所以如果想要List也随之而改变的话,可以在相应的方法里面调用一下的方法:
//下面这个方法就是把界面的DataGridView里的数据源BindingList转换为List。
private IList<Student> BindingListToList()
{
IList< Student > list = new List< Student >((BindingList< Student >)this.dgv.DataSource);
return list;
}
DataGridView绑定泛型List时,利用BindingList来实现增删查改的更多相关文章
- 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性
基于.net的分布式系统限流组件 在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...
- C# DataGridView绑定List对象时,利用BindingList来实现增删查改
当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGridView的数据会随之改变,无需重新绑定到DataGridView. 当Da ...
- 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- 在MVC中使用泛型仓储模式和依赖注入实现增删查改
标签: 原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository ...
- java 利用注解实现BaseDao 增删查改
第一步,编写两个注解类,用于表明实体类对应的表名及字段. TableInfo.java 此注解用于标注表名及主键名 import static java.lang.annotation.Element ...
- 5.在MVC中使用泛型仓储模式和工作单元来进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- 在MVC中使用泛型仓储模式和工作单元来进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- winform datagridview 绑定泛型集合变得不支持排序的解决方案
原文:winform datagridview 绑定泛型集合变得不支持排序的解决方案 案例: 环境:Winform程序 控件:Datagridview 现象:Datagridview控件绑定到List ...
- WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...
随机推荐
- 递归遍历JSON树
递归遍历JSON树 前几天有个人问我,json串的层级无限深,但在json串中的key是已知的,在json串中的value,有些事Object,有些是Array,如何把这些层级无限深的key所对应的v ...
- 山东BOSS性能压力测试
1. 概述 在山东BOSS性能压力测试过程中,发现脚本对于整个压力测试过程的重要性,一个压力测试脚本录制和编辑修改得怎么样直接影响后面压力测试的执行.通常情况下,脚本应尽可能的精简,就像写代码一样.针 ...
- 文件权限之facl丶文件属性丶特殊权限
(1)facl:文件的访问控制列表 作用:对象目录或文件可以对不同的用户设定不同的权限 1)getfacl:查看文件或目录的访问控制列表权限 查看 getfacl file/dir acl权限特征:如 ...
- 跨域请求方式之Jsonp形式
在浏览器端才有跨域安全限制一说,而在服务器端是没有跨域安全限制的. 在两个异构系统(开发语言不同)之间达到资源共享就需要发起一个跨域请求. 而浏览器的同源策略却限制了从一个源头的文档资源或脚本资源与来 ...
- 最小生成树算法详解(prim+kruskal)
最小生成树概念: 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 最小生成树可以用kruskal(克鲁斯卡尔)算法或prim(普里 ...
- MySQL workbench中的PK,NN,UQ,BIN,UN,ZF,AI说明
- PK: primary key (column is part of a pk) 主键- NN: not null (column is nullable) 是否为空 (非空)- UQ: uni ...
- 用Chrome在手机上调试本地网页代码
本文摘自Google 原文地址1:https://developers.google.com/web/tools/chrome-devtools/remote-debugging/?utm_sourc ...
- 关于windows环境下cordova命令行无法启动adb.exe的解决办法
使用phonegap开发手机APP,常常需要更改代码之后进行调试,使用安卓模拟器每次启动非常缓慢,而且不能保证最终在真机上的效果.所以一般都采用真机进行调试. 搭建真机的调试环境这里就不再赘述了,网上 ...
- [BZOJ3990][SDOI2015]排序(DFS)
3990: [SDOI2015]排序 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 902 Solved: 463[Submit][Status][ ...
- JZYZOJ1524 [haoi2012]外星人 欧拉函数
http://172.20.6.3/Problem_Show.asp?id=1524 大概可以算一个结论吧,欧拉函数在迭代的时候,每次迭代之后消去一个2,每个非2的质因子迭代一次又(相当于)生成一个2 ...