解决Gridview在未绑定数据源时进行分页的代码

/ 0评 / 0
private void BindData()
        {
            GridView1.Visible = true;
            try
            {
                 conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    string cmd = "select * from charge";

                    SqlDataAdapter ad = new SqlDataAdapter(cmd, conn);

                    DataSet ds = new DataSet();
                    ad.Fill(ds);
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                    conn.Close();
                }
            }
            catch (System.Exception ea)
            {
             }
        }

protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            GridViewRow bottomPagerRow = GridView1.BottomPagerRow;
            Label bottomLabel = new Label();
            bottomLabel.Text = "目前所在分页:(" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + ")";
            bottomPagerRow.Cells[0].Controls.Add(bottomLabel);

        }

 

以上可以作为参考代码,本人今天研究了好久的咧~哈,大家试试吧,应该可以的~

睡觉去咯~不早了~

发表评论

邮箱地址不会被公开。 必填项已用*标注