子风.NET 进阶中......

路途多艱,唯勤是岸

 

GridView1 与 分页控件一起使用

分页控件平时都是这样用的
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 要设置为一样.

posted on 2007-05-31 15:12  子风  阅读(586)  评论(1编辑  收藏  举报

导航