【问题标题】:The data source does not support server-side data paging数据源不支持服务器端数据分页
【发布时间】:2010-12-12 06:56:37
【问题描述】:

我的屏幕上有一个 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);
}

但是我收到此错误:

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

我做错了什么?

【问题讨论】:

    标签: c# asp.net linq sorting gridview


    【解决方案1】:

    结果变量上的简单ToList() 应该可以工作。

    编辑: 正如我的答案下面的 cmets 中所解释的,错误的原因是数据源应该实现 ICollection。 IEnumerable 不会,当您执行 ToList() 时,它会将其转换为实现 ICollection 的列表。

    【讨论】:

      【解决方案2】:

      您也可以使用通用List&lt;T&gt;。见示例代码sn -p:

      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。

      【讨论】:

        【解决方案3】:

        .ToList() 在数据源的末尾,我正在为我分配工作,如下所示:

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

        【讨论】:

          【解决方案4】:

          我已将代码更改为:

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

          【讨论】:

            【解决方案5】:

            在 ObjectDataSource 中添加 enablePaging="true" 即可。

            【讨论】:

              【解决方案6】:

              LINQ 查询:

              ProductController.cs:

              List<Product> products= productModel.GetProducts(start, offset);
              

              ProductModel.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();
              }
              

              【讨论】:

                【解决方案7】:

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

                此错误的解决方法是使用通用列表集合、数据表、数据集等数据源来绑定 GridView。

                【讨论】:

                  【解决方案8】:

                  试试这篇文章 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>
                  

                  【讨论】:

                    猜你喜欢
                    • 2012-03-12
                    • 2014-08-20
                    • 2012-03-31
                    • 2018-09-12
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-08-06
                    相关资源
                    最近更新 更多