数据源不支持服务器端数据分页

时间:2009-11-02 13:19:23

标签: c# asp.net linq sorting gridview

我的屏幕上有一个GridView,需要它才能进行分页。

标记:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
  AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
  <Columns>
    <asp:BoundField DataField="appID" HeaderText="appID" SortExpression="appID" />
  </Columns>
</asp:GridView>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
  SelectMethod="GetBookingId" 
  TypeName="AppointmentRepository">
  <SelectParameters>
    <asp:Parameter Name="maximumRows" Type="Int32" />
    <asp:Parameter Name="startRowIndex" Type="Int32" />
  </SelectParameters>
</asp:ObjectDataSource>

代码隐藏:

ObjectDataSource1.SelectParameters["maximumRows"].DefaultValue = "10";
ObjectDataSource1.SelectParameters["startRowIndex"].DefaultValue = "0";

LINQ查询:

public IQueryable<tblAppointment> GetBookingId(int maximumRows, int startRowIndex)
{
    var result = (FROM a IN dc.tblAppointments
                  SELECT a).Skip(startRowIndex).Take(maximumRows);
}

但是我收到此错误:

  

数据源不支持服务器端数据分页。

我做错了什么?

8 个答案:

答案 0 :(得分:125)

结果var上的简单ToList()应该有效。

修改: 正如 BornToCode 在我的回答下面的评论中解释的那样,错误的原因是数据源应该实现ICollection。 IEnumerable没有,当你执行ToList()时,它会将其转换为实现ICollection的列表。

答案 1 :(得分:3)

您也可以使用通用List<T>。请参阅示例代码段:

public List<Company> GetContactList(int startindex)
{

    string path = Server.MapPath("~/contacts.xml");
    XDocument xd = XDocument.Load(path);
    IEnumerable<Company> results = (from items in xd.Elements("Company").Elements("Contact")
                   select new Company
                   {
                       Id = items.Element("ID").Value,
                       Photo = (string)items.Element("photo").Value,
                       Name = (string)items.Element("Name").Value,
                       BloodGroup = (string)items.Element("Bg").Value,
                       Dob = (string)items.Element("dob").Value,
                       Anniversery = (string)items.Element("avd").Value,
                       Mobile = (string)items.Element("cnum").Value,
                       designation = (string)items.Element("desig").Value,
                       Team = (string)items.Element("team").Value
                   }).Skip(startindex*10).Take(10);
    return (List<Company>) results;
}

您也可以使用DataSet / DataTable而不是DataReader。

答案 2 :(得分:1)

我已将代码更改为:

public List<string> ListofNewsTitle()
{
    var query = from n in db.NewsEvents
                orderby n.NewsDate descending
                select n.NewsTitle;
    return query.ToList();        
}

答案 3 :(得分:1)

数据源末尾的

.ToList(),我正在为我分配如下工作:

gvCaseLabelsLeft.DataSource = caseLabelsList.OrderBy(c=>c.caseLabelNumber).ToList();

答案 4 :(得分:0)

在ObjectDataSource中,只需添加可用的enablePaging="true"

答案 5 :(得分:0)

LINQ查询:

ProductController.cs:

public List<Product> GetProducts(int start, int offset)
{
    IEnumerable<Product> query = from m in db.Products
                                 orderby m.Id descending
                                 select m;
    query = query.Skip(start).Take(offset);
    return query.ToList();
}

ProductModel.cs:

<div id="services" class="services">

         <p> Services </p>
        <div class="service-grid" style="padding:30px">
                    <div class="">
                        <span> </span>
                    </div>
                    <img src="images/software.png" alt="" style=""/>
                    <h4>Enterprise Applications</h4>
                    <p>We know each and every nook and corner of the IT field. </p>
                </div>
            </div>

答案 6 :(得分:0)

如果您正在使用SqldataReader,那么它不支持分页。

此错误的解决方案是使用Generic List集合,DataTables,DataSet等DataSource来绑定GridView。

答案 7 :(得分:0)

尝试这篇文章 https://www.aspsnippets.com/Articles/ASPNet-GridView-The-data-source-does-not-support-server-side-data-paging.aspx

总之尝试使用这些行

private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, ContactName, Country FROM Customers"))
        {
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                GridView1.DataSource = sdr;
                GridView1.DataBind();
            }
            con.Close();
        }
    }
}

protected void OnPaging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPaging">
<Columns>
    <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
    <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
    <asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>