DataGridView如何绑定DataRow对象集合
DataGridView对象是我们在进行Winform程序开发中经常使用的呈现数据的控件,而数据则是通过DataSource这个Property来设置的。根据MSDN的说明,DataGridView对象支持标准的Windows窗体数据绑定模型,任何实现了下面几种接口的对象都可以作为其数据源:
- IList接口,包括一维数组。
- IListSource接口,比如DataTable,DataSet类。
- IBindingList接口,比如BindingList<T>类。
- IBindingListView接口,比如BindingSource类。
DataTable对象通常被用来作为数据源直接绑定到Grid控件上,比如,
DataTable dtEmployees = new DataTable();
dataGridView1.DataSource = dtEmployees;
有的时候我们需要选择出表中的部分行作为数据源,当然我们可以通过设置DataTable的DefaultView属性过滤给定条件的记录,
DataTable gridTable = (DataTable) dataGrid1.DataSource; // Set the RowFilter to display a company names that
// begin with A through I..
gridTable.DefaultView.RowFilter = "Age < 30";
但是,有的时候我们会用DataTable.Select方法提取出给定条件的DataRow集合数组,如果想直接把这个List<DataRow>作为数据源,最终会呈现几列没有意义的数据(RowError, RowState, Table, and HasErrors)。
实际上,我们也无法将一个IEnumerable<T>对象(比如,一个LINQ查询)作为数据源。 LINQ查询, 必须调用.ToList(). 或.ToArray()方法将其转换成非泛型的对象。
为什么无法将List<DataRow>对象作为数据源呢?
Databinding is controlled by the ListBindingHelper and TypeDescriptor classes. When you bind to a list, the ListBindingHelper.GetListItemProperties method is called to get the columns in the list. If the list implements the
ITypedListinterface, itsGetItemPropertiesmethod is called. Otherwise, it will use TypeDescriptor to get the properties of the first item in the list. (this uses reflection)The DataView class (which DataTable also binds through, using
IListSource) implementsITypedListand returns DataColumnPropertyDescriptors that expose the columns in the table. This is why you can bind to a DataView or DataTable and see columns. However, when you bind to aList<DataRow>, there is noITypedListthat can return the columns as properties. It therefore falls back on reflection and shows the physical properties of theDataRowclass.
上面是一位大人的解释,http://blog.slaks.net/2011/01/binding-to-lists-of-datarows.html/
他给的解决方案是,将List<DataRow>封装在一个实现了ITypedList接口的DataView类对象里。
To solve this issue, you need to wrap the list in a DataView so that you can take advantage of its
ITypedListimplementation. You can do that using theAsDataView()method. This method is only available on theDataTableandEnumerableRowCollection<T>classes; it cannot be called on an arbitrary LINQ query. You can only get anEnumerableRowCollection<T>by calling special versions of the Cast, OrderBy, Where, and Select methods from a DataTable.
我们直接看例子,
DataTable dtEmployee = buildMockTable();
DataRow[] drEmployees = dtEmployee.Select("emp_age <= 50"); List<DataRow> list = drEmployees.ToList<DataRow>();
dgvEmployees.DataSource = dtEmployee.AsEnumerable().Where(list.Contains).AsDataView();
// dgvEmployees.DataSource = dtEmployee.AsEnumerable().CopyToDataTable();
当然也可以调用CopyToDataTable方法,但这样会深拷贝所有的DataRow, 所以如果想更新数据或向让用户看到原始数据上的变化时就比较麻烦。
还有一种方法就是将List<DataRow>加到一个新的表里面,然后将这个表作为绑定数据源。
DataTable dtEmployee = buildMockTable();
DataRow[] drEmployees = dtEmployee.Select("emp_age > 50"); DataTable filteredTable = new DataTable("Employee");
filteredTable = dtEmployee.Copy();
filteredTable.Rows.Clear(); foreach (DataRow row in drEmployees)
{
filteredTable.ImportRow(row);
}
filteredTable.AcceptChanges();
dgvEmployees.DataSource = filteredTable.DefaultView;
这种方法类似CopyToDataTable
Source Code: DataSourceWithDataRow
References
- http://blog.slaks.net/2011/01/binding-to-lists-of-datarows.html/
- http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx
- http://www.dreamincode.net/forums/topic/242000-how-to-fill-a-datagridview-from-datarow-array/
DataGridView如何绑定DataRow对象集合的更多相关文章
- 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性
基于.net的分布式系统限流组件 在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...
- 【转】给DataTable和DataRow扩展方法,直接转换为对象集合或对象
/// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为对象集合或对象 /// 补充说明:此扩展类可以极大的简化操作,但是性能低下,大数据以 ...
- 再谈使用Emit把Datatable转换为对象集合(List<T>)
一.前因和存在的问题 前面我写了一篇<使用Emit把Datatable转换为对象集合(List<T>)>的博文,其实起源于我自己编写的一个orm工具(见前面几篇博文有介绍),里 ...
- 使用Emit把Datatable转换为对象集合(List<T>)
Emit生成动态方法部分摘自网上,但是经过修改,加入了对委托的缓存以及类结构的调整,使之调用更简洁方便.大致的思路是:要实现转换datatable到某个指定对象的集合,本质是实现转换一个datarow ...
- ASP.NET Core的配置(3): 将配置绑定为对象[上篇]
出于编程上的便利,我们通常不会直接利用ConfigurationBuilder创建的Configuration对象读取某个单一配置项的值,而是倾向于将一组相关的配置绑定为一个对象,我们将后者称为Opt ...
- ItemArray DataRow对象的RowState和DataRowVersion属性特点
DataTable.Rows[i].ItemArray DataTable.Rows表示所有行的集合DataTable.Rows[i]加上下标表示其中某一行DataTable.Rows[i].Item ...
- [ASP.NET Core 3框架揭秘] 配置[4]:将配置绑定为对象
虽然应用程序可以直接利用通过IConfigurationBuilder对象创建的IConfiguration对象来提取配置数据,但是我们更倾向于将其转换成一个POCO对象,以面向对象的方式来使用配置, ...
- ASP.NET Core的配置(3): 将配置绑定为对象[下篇]
我们在<读取配置信息>通过实例的形式演示了如何利用Options模型以依赖注入的方式直接获取由指定配置节绑定生成的Options对象,我们再次回顾一下当初我们编写的程序.如下面的代码片段所 ...
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...
随机推荐
- Redis went away
问题过程 输入法业务于12月12日上线词库推送业务,根据用户uuid(uuid平台校验)进行词库推送,在12月17日早上8点多开始出现大量的php报错(Redis went away),报错导致了大量 ...
- 使用spring的JavaMail发送邮件
以前我们使用JavaMail发送邮件,步骤挺多的.现在的项目跟Spring整合的比较多.所以这里主要谈谈SpringMail发送. 导入jar包. 配置applicationContext-email ...
- 用MSBuild和Jenkins搭建持续集成环境(1)[收集]
你或其他人刚刚写完了一段代码,提交到项目的版本仓库里面.但等一下,如果新提交的代码把构建搞坏了怎么办?万一出现编译错误,或者有的测试失败了,或者代码不符合质量标准所要求的底限,你该怎么办? 最不靠谱的 ...
- JS简单验证
1.验证是否全为数字 2.验证邮箱 3验证手机号 4.验证身份证号 5.验证时间格式 下面是代码,可直接用,有注释 <html> <meta charset="utf-8& ...
- 记录搭建Odoo框架
一.获取 Odoo 源码 Odoo 是一个开源项目,我们可以轻松的在 Github 上找到它的源码.本次中使用的是 12.0 版本的 Odoo,所以在拉取代码时选择 12.0 的分支.确保拉取的速度, ...
- CSS3 鼠标划上图片放大
td img{transition: all 1s}/*鼠标划上,图片1s全部显示完成*/ td img:hover{ transform: scale(5) translateX(50%) tran ...
- JQuery判断数组中是否包含某个字符串
var arry = [ "C#", "html", "css", "JavaScript" ]; var result ...
- 【jdk源码2】Objects源码学习
在学习上一个类TreeMap的时候,提到了这个类,这个类是jdk1.7新增的,里面有很多实用的方法.就是一个工具类,熟悉以后,如果里面有已经实现的方法,那么就不要再去实现了,省时省力省测试. 一.简单 ...
- springcloud 入门 6 (断路器hystrix)
hystrix:断路器 断路器是为了解决服务故障的“雪崩”, 雪崩是指,由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请 ...
- Oracle表字段的增删改和重命名
增加字段语法:alter table tablename add (column datatype [default value][null/not null],….); 说明:alter table ...