I have a GridView on my screen and need it to allow paging.
Markup:
.ToList()
at the end of the DataSource, I am assigning worked for me like below:
gvCaseLabelsLeft.DataSource = caseLabelsList.OrderBy(c=>c.caseLabelNumber).ToList();
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>
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.
In ObjectDataSource just add enablePaging="true"
that will work.
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.
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();
}