2.0中GridView中的分页

本文介绍了一种使用 C# 和 SQL 进行分页查询的方法。通过获取配置文件中的连接字符串,建立数据库连接,并使用 SqlDataAdapter 填充 DataSet 中的数据。然后利用 PagedDataSource 控件实现数据分页显示,包括当前页数、总页数的显示及前后页链接的生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  string connectionString = ConfigurationManager.ConnectionStrings["xinProductsConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString=connectionString;
        string sqlstr = "select * from BoardRoom";
        SqlDataAdapter da=new SqlDataAdapter(sqlstr,conn);
        DataSet ds=new DataSet();

        try
        {
            da.Fill(ds, "word");
            PagedDataSource pg = new PagedDataSource();
            pg.DataSource = ds.Tables["Word"].DefaultView;
            pg.AllowPaging = true;
            pg.PageSize = 5;
            int currPage;
            if (Request.QueryString["page"] != null)

                currPage = Convert.ToInt32(Request.QueryString["page"]);
            else
                currPage = 1;

            pg.CurrentPageIndex = currPage - 1;

            LabelCurrpage.Text = "当前页:第" + currPage.ToString() + "页";
            LabelTotal.Text = "共有:" + pg.PageCount.ToString() + "页";

            HyperLinkFirstPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page="+Convert.ToInt32(1);
            if (!pg.IsFirstPage)
                HyperLinkNextPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToInt32(currPage - 1);
            if (!pg.IsLastPage)
                HyperLinkPrePage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToInt32(currPage + 1);
            HyperLinkLastPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToInt32(pg.PageCount);
          
            GridView1.DataSource = pg;
            GridView1.DataBind();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值