DataGridView如何绑定DataRow对象集合
2013-08-22 16:24
176 查看
DataGridView对象是我们在进行Winform程序开发中经常使用的呈现数据的控件,而数据则是通过DataSource这个Property来设置的。根据MSDN的说明,DataGridView对象支持标准的Windows窗体数据绑定模型,任何实现了下面几种接口的对象都可以作为其数据源:
IList接口,包括一维数组。
IListSource接口,比如DataTable,DataSet类。
IBindingList接口,比如BindingList<T>类。
IBindingListView接口,比如BindingSource类。
DataTable对象通常被用来作为数据源直接绑定到Grid控件上,比如,
有的时候我们需要选择出表中的部分行作为数据源,当然我们可以通过设置DataTable的DefaultView属性过滤给定条件的记录,
但是,有的时候我们会用DataTable.Select方法提取出给定条件的DataRow集合数组,如果想直接把这个
实际上,我们也无法将一个
为什么无法将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
The DataView class (which DataTable also binds through, using
上面是一位大人的解释,http://blog.slaks.net/2011/01/binding-to-lists-of-datarows.html/
他给的解决方案是,将List<DataRow>封装在一个实现了
To solve this issue, you need to wrap the list in a DataView so that you can take advantage of its
我们直接看例子,
当然也可以调用CopyToDataTable方法,但这样会深拷贝所有的DataRow, 所以如果想更新数据或向让用户看到原始数据上的变化时就比较麻烦。
还有一种方法就是将List<DataRow>加到一个新的表里面,然后将这个表作为绑定数据源。
这种方法类似CopyToDataTable
Source Code: DataSourceWithDataRow
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/
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(). 或[code].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, its
GetItemPropertiesmethod 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) implements
ITypedListand 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 a
List<DataRow>, there is no
ITypedListthat can return the columns as properties. It therefore falls back on reflection and shows the physical properties of the
DataRowclass.
上面是一位大人的解释,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 the
AsDataView()method. This method is only available on the
DataTableand
EnumerableRowCollection<T>classes; it cannot be called on an arbitrary LINQ query. You can only get an
EnumerableRowCollection<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/
相关文章推荐
- Silverlight中的TabControl如何绑定数据?重写tabcontrol和tabItem 解决绑定友好问题。可以绑定对象集合
- spring mvc 如何从前台表单传递集合参数并绑定集合对象。 [问题点数:40分,结帖人NewMoons]
- 如何:访问绑定到 Windows 窗体 DataGridView 行的对象
- 如何绑定IList集合中对象的复合属性到DataGrid中。
- [WinForm]DataGridView如何绑定对象的复合属性
- 如何在已经绑定数据的dataGridView对象中动态添加记录?
- list集合绑定在datagridview上时如何实现排序
- DataGridViewComboBoxColumn 绑定对象集合时存在BUG?
- 如何在已经绑定数据的dataGridView对象中动态添加记录?
- 如何通过for循环添加对象到集合(防止数据覆盖)
- WinForm程序用使用List对象绑定DataGridView数据源
- Objective-C如何使用对象集合学习系列二
- 文本框textbox如何同datagridview绑定
- DataGridView绑定Ilist对象,出现IndexOutOfRangeException错误的解决方法
- 一步一步教你使用AgileEAS.NET基础类库进行应用开发-WinForm应用篇-演示ORM对象与DataGridView的绑定技术-商品字典的另一个实现
- spring3mvc如何把checkbox的值绑定到model对象的int数据类型
- 关于集合中对象如何进行排序
- “当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行。” 的解决一法
- SilverLight-DataBinding:二、Bingding to a Collection Objects(绑定一个集合对象)
- ASP.NET MVC之表单集合数据自动绑定到对象属性(集合)中