在gridview中分页

时间:2011-06-03 08:59:55

标签: c# asp.net .net gridview page-index-changed

我的gridView:

              <asp:GridView ID="gridView1" runat="server" CellPadding="4" AllowPaging="true" PageSize="5"  emptydatatext="No data available." 
                    CssClass="datagrid" 
                    ForeColor="#333333" GridLines="None" 
                    onrowcreated="gridView1_RowCreated" 
                    onpageindexchanging="gridView1_PageIndexChanging">

                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>

我的代码隐藏:

    protected void gridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridView1.PageIndex = e.NewPageIndex;
        BindDataGrid();
    }



    protected void BindDataGrid()
    {

        DataSet ds = new DataSet();

        ds = dbM.GetInfo(name, mobilePhone, info1); //get info to BD and save in "ds"

        gridView1.DataSource = ds;
        gridView1.DataBind();
    }

并且“分页”不起作用。它显示前5行,但不显示分页数

出错了???

请帮忙

感谢

3 个答案:

答案 0 :(得分:2)

尝试在GridView标记中添加一些寻呼机设置

<PagerSettings Mode="NextPreviousFirstLast" Position="TopAndBottom" />

如果可行,您可以将其更改为所需的外观。有many choices,包括

  • FirstPageImageUrl
  • FirstPageText
  • LastPageImageUrl
  • LastPageText
  • 模式(例如数字或 NumericFirstLast)
  • NextPageImageUrl
  • NextPageText
  • PageButtonCount
  • 位置
  • PreviousPageImageUrl
  • PreviousPageText
  • Visible`

答案 1 :(得分:2)

PagerSettings - 属性添加到GridView-Markup并将其设置为True

PagerSettings-Visible="true"

编辑:但默认情况下应为true,所以我不确定这是否会改变任何内容

答案 2 :(得分:2)

 Use **"PageIndexChanged"**

protected void gridView1_PageIndexChanged(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gridView1.PageIndex = e.NewPageIndex;
                BindDataGrid();
    ;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }