在asp.net中使用linq进行Gridview分页

时间:2010-10-05 05:41:41

标签: asp.net linq gridview paging

我正在从sql数据库中检索数据。我想拆分记录并将其绑定在三个不同的网格中。在应用分页之前,一切正常。我收到数据源不支持服务器端数据分页。错误

代码:

 DataTable  StoreDisplayTypeList = storeDisplayBL.GetStoreDisplayDetails();

        var specialBook = from myRow in StoreDisplayTypeList.AsEnumerable() where (string)myRow["StoreDisplayType"] == "Special Book" select myRow;
        var newRelease = from myRow in StoreDisplayTypeList.AsEnumerable() where (string)myRow["StoreDisplayType"] == "New Release" select myRow;
        var Seller = from myRow in StoreDisplayTypeList.AsEnumerable() where (string)myRow["StoreDisplayType"] == "Best Seller" select myRow;
        var featuredEdition = from myRow in StoreDisplayTypeList.AsEnumerable() where (string)myRow["StoreDisplayType"] == "Featured Edition" select myRow;         

        this.gvBestSeller.DataSource = Seller;
        this.gvBestSeller.DataBind();  // Error

        this.gvNewRelease.DataSource = newRelease;
        this.gvNewRelease.DataBind();  // Error

        this.gvSpecialBook.DataSource = specialBook;
        this.gvSpecialBook.DataBind();  // Error

        this.gvFeaturedREdition.DataSource = featuredEdition;
        this.gvFeaturedREdition.DataBind();  // Error

1 个答案:

答案 0 :(得分:2)

那是因为你正在使用vars。您需要使用强类型对象,例如List<>。

public class Book
{
    public string _StoreDisplayType { get; set; }
    public string _title { get; set; }
}

var specialBook = from myRow in StoreDisplayTypeList.AsEnumerable() 
                  where (string)myRow["StoreDisplayType"] == "Special Book" 
                  select new Book{ 
                      _StoreDisplayType = myRow["StoreDisplayType"].ToString(), 
                      _title = myRow["Title"].ToString()
                  };      

this.gvSpecialBook.DataSource = specialBook.ToList();
this.gvSpecialBook.DataBind();