gpt4 book ai didi

c# - 将一组数据行绑定(bind)到 datagridview

转载 作者:行者123 更新时间:2023-11-30 22:16:20 27 4
gpt4 key购买 nike

我试过下面的代码,但datagridview中没有任何显示。

有什么建议吗?

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

最佳答案

来自 MSDN

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

The IList interface, including one-dimensional arrays.

The IListSource interface, such asthe DataTable and DataSet classes.

The IBindingList interface, such asthe BindingList class.

The IBindingListView interface, such as theBindingSource class.

由于与数组进行数据绑定(bind)的默认行为,您无法为 DataSource 属性设置 Datatable.Select 方法将返回的数据行数组。来自 this post :

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

因此,另一种方法是创建一个新的 DataTable,使用 Clone 方法从 DataTable 源获取架构,并使用 DataRows 数组填充新的 DataTable。

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

或者,您也可以通过 BindingSource 对象进行绑定(bind)并使用其 Filter 属性。

关于c# - 将一组数据行绑定(bind)到 datagridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17468390/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com