将一组数据行绑定到datagridview [英] binding a set of datarows to datagridview

查看:156
本文介绍了将一组数据行绑定到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码,但datagridview中没有显示。



任何建议?

  string strFilterOption =dtcolnPurchaseProductExpProductNo = 270; 
dgvProductExp.DataSource = dtPurchaseProductExp.Select(strFilterOption);


解决方案

MSDN


DataGridView类支持标准的Windows Forms
数据绑定模型。这意味着数据源可以是
实现以下接口之一的任何类型:



IList界面,包括一维数组。



IListSource接口,如
的DataTable和DataSet类。



IBindingList接口,如
的BindingList类。



IBindingListView界面,如
BindingSource类。


由于使用数组进行数据绑定的默认行为,您无法为DataSource设置属性,Datatable.Select方法将返回的Datarow数组。从此帖


主要的是对象实现IList或者是
1维数组。事情是,一个数组具有默认行为
用于数据绑定 - 反射。数组中的对象类型反映为
,用于发现其不被索引的公共属性,并且
具有可以在网格中表示的值类型。然后,当数据绑定发生时,这些
属性将用作字段。
由于数组中的数据类型是DataRow,有六个public
属性:HasErrors,Item,ItemArray,RowError,RowState和
表。但Item是一个索引属性,ItemArray的类型为
Object(),它不能在gridview中显示,因此这两个
属性将被忽略,而另外四个在网格中显示。 / p>

所以另一种方式将是创建一个新的DataTable,使用Clone方法从DataTable源获取模式,并填充新数据表与DataRows数组。

  string strFilterOption =dtcolnPurchaseProductExpProductNo = 270; 
DataTable cloneTable;
cloneTable = dtPurchaseProductExp.Clone();
foreach(dtPurchaseProductExp.Select(strFilterOption)中的DataRow行)
{
cloneTable.ImportRow(row);
}
dgvProductExp.DataSource = cloneTable;

或者,您还可以通过BindingSource对象进行绑定,并使用其Filter属性。 >

I have tried the following code, but nothing is displayed in datagridview.

Any Suggestions?

string strFilterOption = "dtcolnPurchaseProductExpProductNo=270";
dgvProductExp.DataSource = dtPurchaseProductExp.Select(strFilterOption);

解决方案

From MSDN

The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces:

The IList interface, including one-dimensional arrays.

The IListSource interface, such as the DataTable and DataSet classes.

The IBindingList interface, such as the BindingList class.

The IBindingListView interface, such as the BindingSource class.

Due to the default behavior for databinding with array, you can't set, for the DataSource property, the array of Datarows that the Datatable.Select method will return. From this post:

The primary one is that the object implements IList or is a 1-Dimensional Array. The thing is, an array has a default behavior for databinding - Reflection. The object type within the array is reflected to discover its public properties which are not indexed and with value types that can be represented in the grid. These properties are then used as the Fields when the databinding occurs. Since the data type in your array is DataRow, there are six public properties: HasErrors, Item, ItemArray, RowError, RowState, and Table. But Item is an indexed property and ItemArray is of type Object(), which can't be displayed in a gridview, so these two properties are ignored and the other four are shown in the grid.

So another way would consist to create a new DataTable, using Clone method in order to get the schema from the DataTable source, and populate the new DataTable with the array of DataRows.

string strFilterOption = "dtcolnPurchaseProductExpProductNo=270";
DataTable cloneTable;
cloneTable = dtPurchaseProductExp.Clone();
foreach (DataRow row in dtPurchaseProductExp.Select(strFilterOption))
{
   cloneTable.ImportRow(row);
}
dgvProductExp.DataSource = cloneTable;

Or, you can also do your binding through a BindingSource object and use its Filter property.

这篇关于将一组数据行绑定到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆