问题
I have a GridView, and I want to implement Pagination functionality. This is working fine.
protected DataSet FillDataSet()
{
string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
con = new SqlConnection(source);
cmd = new SqlCommand("proc_mygrid", con);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
return ds;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int newPagenumber = e.NewPageIndex;
GridView1.PageIndex = newPagenumber;
GridView1.DataSource = FillDataSet();
GridView1.DataBind();
}
But the problem is for each pagination I have to call FillDataSet();. Is there any way to stop that. Any other coding approaches?
回答1:
Have a look at Scott Mitchell's article: Custom Paging in ASP.NET 2.0 with SQL Server 2005.
If you're using a version of SQL Server pre-2005, then try: A More Efficient Method for Paging Through Large Result Sets
回答2:
You could also consider using LINQ To SQL as it makes it easy to implement server-side pagingation.
来源:https://stackoverflow.com/questions/1248447/grid-view-pagination