分页控件平时都是这样用的
da.Fill(ds, (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize, this.AspNetPager1.PageSize, "t1");
直接从数据库中抓到你所需要的数据,然后你在填充GridView1.
但是现在我的数据源不是sql2000 而是自己填充的一个DataTable 了,以dt做为数据源.那问题来了,前面的那个函数显然不行了,那分页,怎么办呢?
解决方法如下:
直接绑定:
private void bind()
{
DataTable dt = (DataTable)Session["t1"];
this.AspNetPager1.RecordCount = ((DataTable)Session["t1"]).Rows.Count;
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
在分页控件里面的PageChanged里面要写事件,指定GridView1的当前页的索引.
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
this.GridView1.PageIndex = this.AspNetPager1.CurrentPageIndex - 1;
this.bind();
}
注意了: AspNetPager 和GridView1的PageSize 要设置为一样.
转载于:https://www.cnblogs.com/86188281/archive/2007/05/31/766484.html