Loading

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 ITypedList interface, its GetItemProperties  method 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 ITypedList and 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 ITypedList that can return the columns as properties.  It therefore falls back on reflection and shows the physical properties of the DataRow class.

上面是一位大人的解释,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 ITypedList implementation.  You can do that using the AsDataView() method.  This method is only available on the DataTable  and 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

posted @ 2013-08-22 16:24  光脚码农  阅读(2236)  评论(0编辑  收藏  举报