C# DataTable。选择作为DataList的数据源

C# DataTable。选择作为DataList的数据源,c#,datalist,C#,Datalist,我正在尝试过滤数据源中的数据,该数据源打印在datalist中。我知道如何使用datalist,这没有问题。问题在于过滤 我试过这个: DataSet ds = (DataSet)Application["Products"]; DataSet newDS = new DataSet(); newDS.Tables.Add("products"); DataRow[] DR = ds.Tables[0].Select("CategoryID='" + this.CategoryID + "'"

我正在尝试过滤数据源中的数据,该数据源打印在datalist中。我知道如何使用datalist,这没有问题。问题在于过滤

我试过这个:

DataSet ds = (DataSet)Application["Products"];
DataSet newDS = new DataSet();
newDS.Tables.Add("products");

DataRow[] DR = ds.Tables[0].Select("CategoryID='" + this.CategoryID + "'");
for (int i = 0; i < DR.Length; i++)
    newDS.Tables[0].ImportRow(DR[i]);

PagedDataSource PDS = new PagedDataSource();
PDS.DataSource = newDS.Tables[0].DefaultView;
PDS.AllowPaging = true;
PDS.PageSize = 9;
PDS.CurrentPageIndex = CurrentPage;

this.DataList_Products.DataSource = PDS;
this.DataList_Products.DataBind();
DataSet ds=(数据集)应用程序[“产品”];
数据集newDS=新数据集();
新增。表格。添加(“产品”);
DataRow[]DR=ds.Tables[0]。选择(“CategoryID=”“+this.CategoryID+”);
对于(int i=0;i
之后我收到了这个问题:

数据绑定:“System.Data.DataRowView”不包含名为“ProductID”的属性。

我有一个名为ProductID的属性,如何解决这个问题?

新数据集(及其数据表)没有原始数据集的结构

DataSet ds = (DataSet)Application["Products"];
DataSet newDS = new DataSet();
DataTable newTable = ds.Tables[0].Clone();//this copies the structure
newDS.Tables.Add(newTable);
新数据集(及其数据表)没有原始数据集的结构

DataSet ds = (DataSet)Application["Products"];
DataSet newDS = new DataSet();
DataTable newTable = ds.Tables[0].Clone();//this copies the structure
newDS.Tables.Add(newTable);

也许我遗漏了一些东西,但看起来你的代码比你需要的多得多。此外,您还应利用LINQ实现以下目的:

this.DataList_Products.DataSource = ds.Tables[0].AsEnumerable().Where(r => r.Field<int>("CategoryID") == this.CategoryID).AsDataView().ToTable();
this.DataList_Products.DataBind();
this.DataList\u Products.DataSource=ds.Tables[0].AsEnumerable().Where(r=>r.Field(“CategoryID”)==this.CategoryID.AsDataView().ToTable();
this.DataList_Products.DataBind();

也许我遗漏了什么,但看起来您的代码比您需要的多得多。此外,您还应利用LINQ实现以下目的:

this.DataList_Products.DataSource = ds.Tables[0].AsEnumerable().Where(r => r.Field<int>("CategoryID") == this.CategoryID).AsDataView().ToTable();
this.DataList_Products.DataBind();
this.DataList\u Products.DataSource=ds.Tables[0].AsEnumerable().Where(r=>r.Field(“CategoryID”)==this.CategoryID.AsDataView().ToTable();
this.DataList_Products.DataBind();

@JohnSmith很高兴它对你有用。如果你能接受这个答案,那就太好了。@JohnSmith我的意思是,如果这个答案适合你,你应该选择它作为正确的答案。这就是stackoverflow的工作方式。@JohnSmith很高兴它能为您工作。如果你能接受这个答案,那就太好了。@JohnSmith我的意思是,如果这个答案适合你,你应该选择它作为正确的答案。这就是stackoverflow的工作方式。