若想将 List<T>作为DataGridView的数据源,然后后续还想继续操作的话,需要将List<T>赋值给BindingList对象,

然后直接将BindingList赋值给DataGridView.DataSource, 如此直接操作BindingList对象时,DataGridView的结果会动态随之更新。

1,绑定

List<UserClass>  listUserClass = new List<UserClass>();
BindingList   BList<UserClass> ;
listUserClass = this.UserMethodInitList(); //初始化 BList = new BindingList<UserClass>( listUserClass);//赋值给BindingList对象,Form全局变量
this.DataGridView1.DataSource = BList; //将DataGridView里的数据源绑定成BindingList

2, 获取当前选定的行

//获取行对象后
List<UserClass> modiObj = this.DataGridView1.CurrentRow.DataBoundItem as UserClass;

3, 修改当前行

//获取行对象后
List<UserClass> modiObj = this.DataGridView1.CurrentRow.DataBoundItem as UserClass; modiObj .cost = ; //修改值 int pos = this.DataGridView1.CurrentRow.Index; //记位置 this.BList.RemoveAt( pos); //删除行 this.BList.Insert( pos, modiObj );//添加修改后的行到指定位置, 不指定位置默认添加到最后

4,删除行

int  pos = this.DataGridView1.CurrentRow.Index; //记位置

this.BList.RemoveAt( pos); //删除行,操作BindingList对象即可更新DataGridview

5,删除多行

            //允许删除多行

            DataGridViewSelectedRowCollection rows = this.DataGridView1.SelectedRows;

            foreach (DataGridViewRow row in rows)

            {

                this.BList.RemoveAt(row.Index);

            }

6, 返向转换

BindingList<UserClass> Blist = (BindingList<UserClass>) this.DataGridView1.DataSource;

List<UserClass> list1 = List<UserClass>( Blist);

C# 中DataGridView 绑定List<T>做数据源的操作问题的更多相关文章

  1. C# Winform中DataGridView绑定后DataGridViewCheckBoxColumn无法显示的问题

    在控件DataGridView绑定数据源后,发现DataGridViewCheckBoxColumn不能显示当前的check值.经过一番努力,现将完整代码奉献出来,仅供参考. 错误代码: /*禁止自动 ...

  2. c# winform 中DataGridView绑定List<T> 不能显示数据

    遇到问题 DataGridView绑定List后,List更新后再次绑定不显示数据 datagridview 绑定数据源的时候 用List是不能显示修改内容的..要用binginglist<T& ...

  3. C# 中 datagridview 绑定BindingList类型和更新

    C# 中的datagridview是一个非常有用且强大的控件,可以用来绑定数据库.绑定LIST类型的变量等等. 这里我们说一说绑定List类型并实时更新datagridview的情况.实时更新,指的是 ...

  4. winfrom中DataGridView绑定数据控件中DataGridViewCheckBoxColumn怎么选中

    ; i < this.dataGridView1.Rows.Count; i++) { this.dataGridView1.Rows[i].Cells["CheckBoxCulums ...

  5. shell 从文件中读取批量文件名并做命令行操作

    222文件内容: /home/zhangsuosheng/Desktop/9-30/9_30/1bak/1538291162.png /home/zhangsuosheng/Desktop/9-30/ ...

  6. DataGridView绑定数据源的几种方式

    使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...

  7. C# DataGridView绑定数据源的几种常见方式

    开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1. 简单的数据绑定 例1 using (SqlConnection conn = new SqlConnect ...

  8. DataGridView绑定数据源

    给DataGridView绑定数据源比較简单,方法主要有两种: 1.直接在控件属性中绑定数据源,这样的方法最简单,但它是直接连接数据库的,这样就和传DataTable的后果差点儿相同了,所以还是尽量避 ...

  9. 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性

    基于.net的分布式系统限流组件   在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...

随机推荐

  1. Exception error message with incorrect line number

    In Release mode the number in front of the exception is NOT the line of code. Instead it's an offset ...

  2. GC学习笔记

    GC学习笔记 这是我公司同事的GC学习笔记,写得蛮详细的,由浅入深,循序渐进,让人一看就懂,特转到这里. 一.GC特性以及各种GC的选择 1.垃圾回收器的特性 2.对垃圾回收器的选择 2.1 连续 V ...

  3. SAR命令

    前面已经介绍了 vmstat和top命令的解析及使用,下面我们来学习一个更重要的命令sarsar命令可以通过参数单独查看系统某个局部的使用情况 sar 命令行的常用格式: sar [options] ...

  4. Java多线程之阻塞I/O如何中断

    阻塞的I/O线程在关闭线程时并不会被打断,需要关闭资源才能打断. 1.执行socketInput.close();阻塞可中断.2.执行System.in.close();阻塞没有中断. package ...

  5. shell test -n -z

    z --- zero  字符串长度为零 2)判断字符串 test –n 字符串                                   字符串的长度非零 test –z 字符串       ...

  6. crm 4 UserHasRole

    //获取当前人员是否含有指定角色权限 function UserHasRole(roleName) { //get Current User Roles, oXml is an object var ...

  7. C Primer Plus(第五版)4

    第四章 字符串和格式化输入输出 4.2 字符串简介 字符串(character string)就是一个或多个字符的序列.下面是一个字符串的例子: “Zing went the strings of m ...

  8. js捕获回车事件,并且获取每一条输入内容

    <body> <div style="width: 200px; height: 20px;"> <textarea id="inputVa ...

  9. pm剩余要看的内容

    1. thermal profiles tuning, customize thermal daemon for different thermal usage model 2.gas gauge d ...

  10. Codeforces Round #223 (Div. 2) C

    C. Sereja and Prefixes time limit per test 1 second memory limit per test 256 megabytes input standa ...