The data source does not support server-side data paging

后端 未结 8 1183
眼角桃花
眼角桃花 2020-11-29 07:20

I have a GridView on my screen and need it to allow paging.

Markup:



        
相关标签:
8条回答
  • 2020-11-29 07:29

    .ToList() at the end of the DataSource, I am assigning worked for me like below:

    gvCaseLabelsLeft.DataSource = caseLabelsList.OrderBy(c=>c.caseLabelNumber).ToList();
    
    0 讨论(0)
  • 2020-11-29 07:32

    Try this article https://www.aspsnippets.com/Articles/ASPNet-GridView-The-data-source-does-not-support-server-side-data-paging.aspx

    in summary try to use these lines

    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>
    

    0 讨论(0)
  • 2020-11-29 07:42

    You can use generic List<T> also. See the sample code snippet:

    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;
    }
    

    You can also use DataSet/DataTable instead of DataReader.

    0 讨论(0)
  • 2020-11-29 07:45

    In ObjectDataSource just add enablePaging="true" that will work.

    0 讨论(0)
  • 2020-11-29 07:47

    If You are Using SqldataReader then its not support Paging.

    Solution to this error is making use of DataSources such as Generic List collections, DataTables, DataSets, etc. to bind the GridView.

    0 讨论(0)
  • 2020-11-29 07:48

    LINQ query:

    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();
    }
    
    0 讨论(0)
提交回复
热议问题