日期:2014-05-17  浏览次数:20414 次

GridView 序号,分页连续显示
给GridView添加一个序号列,点下一页又从1开始了,怎样让分页后连续显示序号

GridView添加序号
C# code

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex >= 0)
        {
            e.Row.Cells[0].Text = Convert.ToString(e.Row.DataItemIndex + 1);
        }
    }



分页(数据库是oracle)
C# code

int pageCount;//总页数
    int currentPage = 1;//第定义当前页
    private void Bind()
    {
        OracleConnection conn = new OracleConnection(ConnectionString);
        string str = "select * from order order by id desc";
        OracleDataAdapter da = new OracleDataAdapter(str, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = ds.Tables[0];
        //创建数据源
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = dt.DefaultView;
        //允许分页
        pds.AllowPaging = true;
        //设置每页显示记录数
        pds.PageSize = int.Parse("10");
        //获取总页数
        pageCount = pds.PageCount;
        Label2.Text = pageCount.ToString();
        pds.CurrentPageIndex = currentPage - 1;
        //当前页
        Label1.Text = Convert.ToString(currentPage);
        LinkButton1.Enabled = true;
        LinkButton2.Enabled = true;
        LinkButton3.Enabled = true;
        LinkButton4.Enabled = true;
        if (Label1.Text == "1")
        {
            LinkButton1.Enabled = false;
            LinkButton2.Enabled = false;
        }
        if (Label1.Text == Label2.Text)
        {
            LinkButton3.Enabled = false;
            LinkButton4.Enabled = false;
        }
        GridView1.DataSource = pds;
        GridView1.DataBind();        
    }

 //首页
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (this.Label1.Text == "1")
        { }
        else
        {
            currentPage = 1;
            Bind();
        }
    }
    //上一页
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        if (this.Label1.Text != "1")
        {
            currentPage = int.Parse(this.Label1.Text) - 1;
            this.Label1.Text = currentPage.ToString();
            Bind();
        }
    }
    //下一页
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        if (this.Label2.Text == this.Label1.Text)
        {
        }
        else
        {
            currentPage = int.Parse(this.Label1.Text) + 1;
            this.Label1.Text = currentPage.ToString();
            Bind();
        }
    }
    //末页
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        if (this.Label2.Text != this.Label1.Text)
        {
            this.Label1.Text = this.Label2.Text;
            currentPage = int.Parse(this.Label1.Text);
            Bind();
        }
    }



------解决方案--------------------
直接在读取数据的时候加上吧。这个只能显示单页的。

要么就直接在 e.Row.DataItemIndex 判断当前页数 然后进行 pageindex * pagesize + 1