-6

我正在尝试这个但没有成功

var v=ds.Table[0].AsEnumerable().Where<DataRow>(r=>r.Field<int>("productID")==23);
GridView1.DataSource=v;
GridView1.DataBind();

gridview 仅显示一行标题 RowError HasRrror 但我期待一行包含产品信息。

4

2 回答 2

0

试试这个。

var results = from myRow in ds.Table[0].Rows 
              where myRow.Field<int>("productID") == 23
              select myRow;

这应该为您提供您正在寻找的行。

于 2012-05-15T10:51:05.050 回答
0

我认为你应该尝试使用CopyToDataTable

ds.Table[0].AsEnumerable().Where<DataRow>(r=>r.Field<int>("productID")==23).CopyToDataTable();

当您将其转换为 Enumerable 时,过滤后的列表没有标识符,ProductID因此网格将无法映射列并失败。

或者,您也可以选择使用AsDataView()而不是CopyToDataTable().

更详细的解释可以找到Binding DataRows

数据绑定由 ListBindingHelper 和 TypeDescriptor 类控制。绑定到列表时,会调用 ListBindingHelper.GetListItemProperties 方法来获取列表中的列。如果列表实现了 ITypedList 接口,则调用其 GetItemProperties 方法。否则,它将使用 TypeDescriptor 来获取列表中第一项的属性。(这使用反射)

DataView 类(DataTable 也通过它绑定,使用 IListSource)实现 ITypedList 并返回显示表中列的 DataColumnPropertyDescriptors。这就是您可以绑定到 DataView 或 DataTable 并查看列的原因。但是,当您绑定到列表时,没有可以将列作为属性返回的 ITypedList。因此,它依靠反射并显示 DataRow 类的物理属性。

于 2012-05-15T10:59:01.387 回答