如何在动态网格视图中创建分页

时间:2012-11-23 10:02:33

标签: c# gridview

我正在使用C#,在这里我附上我的动态网格视图的部分代码,它的工作正常。我需要在动态网格视图上设置分页,如何调用OnPageIndexChanging

       GridView gv = new GridView();
        gv.AllowPaging = false;
        gv.AlternatingRowStyle.BackColor = System.Drawing.Color.WhiteSmoke;
        gv.GridLines = GridLines.Horizontal;
        gv.CellPadding = 2;
        gv.CellSpacing = 5;
        gv.HeaderStyle.BackColor=System.Drawing.Color.Black;
        gv.HeaderStyle.ForeColor = System.Drawing.Color.White;

        gv.AutoGenerateColumns = false;
        BoundField nameColumn = new BoundField();
        nameColumn.DataField = "fld_id";
        nameColumn.HeaderText = "First Name";
        gv.Columns.Add(nameColumn);
        nameColumn = new BoundField();
        nameColumn.DataField = "fld_name";
        nameColumn.HeaderText = "Last Name";
        gv.Columns.Add(nameColumn);
        nameColumn = new BoundField();
        nameColumn.DataField = "fld_state";
        nameColumn.HeaderText = "Age";
        gv.Columns.Add(nameColumn);

        gv.DataSource = ds;

        gv.DataBind();
        plangrid.Controls.Add(gv);

3 个答案:

答案 0 :(得分:4)

gv.AllowPaging = true;
gv.PageSize =10;
gv.PageIndexChanged+= new EventHandler(grid1_PageIndexChanged);

protected void grid1_PageIndexChanged(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
gv.Databind();
}

答案 1 :(得分:3)

试试这个

gv.AllowPaging = true;
gv.PageSize =10;
gv.PageIndexChanged+= new EventHandler(grid1_PageIndexChanging);

protected void grid1_PageIndexChanged(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
gv.Databind();
}

答案 2 :(得分:1)

尝试设置gw.AllowPaging = true; 然后设置gw.PageSize = when是您想要进入页面的元素数。