Search code examples
c#asp.netgridviewaspxgridviewpager

Show Pager row, but hide page numbers in Gridview


Simple problem.

I have an ASP.net GridView (VS2005) and it has page numbers, but when there is less than the maximum rows per page (< 10), the Pager row disappears. This makes my gridview look ugly, like as if there's a missing row at the bottom.

I can force display the Pager row, but I need to hide the page number 1, because it's obvious we're on PAGE ONE !

                <asp:GridView ID="gvFTUNSENT" runat="server" 
                    AutoGenerateColumns="False" CellPadding="4" ForeColor="Black" AllowSorting="True" CssClass="gvCSS" Width="100%"
                    DataKeyNames="StudentID,StudentUnitID" DataSourceID="sdsFTUNSENT" 
                    GridLines="Vertical" AllowPaging="True" PageSize="10" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
                    OnPreRender="GridView_PreRender" 
                    OnLoad="GridView_Load" 
                    OnRowDataBound="GridView_RowDataBound" >
                    <RowStyle Wrap="True" Height="48px" />
                    <Columns>
...blahblah
                    </Columns>
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle CssClass="cssPager" BackColor="#6B696B" ForeColor="White" HorizontalAlign="Left" Height="100%" />
                    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                    <EmptyDataTemplate>
                        <asp:Label ID="lblNDF1" runat="server" Text="NO DATA FOUND" Font-Size="X-Large" Width="500px" style="text-align:center" />
                    </EmptyDataTemplate>
                    <EmptyDataRowStyle HorizontalAlign="Center" />
                </asp:GridView>

Here I force show the Pager row...

protected void GridView_PreRender(object sender, EventArgs e)
{
    GridView gv = (GridView)sender;

    //keep showing pager line even if there is only one row of data
    GridViewRow gvr = (GridViewRow)gv.BottomPagerRow;
    if (gvr != null)
        gvr.Visible = true;
}

But I don't want to see Page 1 in there, so I tried this ...

if (e.Row.RowType == DataControlRowType.Pager)
{
    //keep showing pager line even if there is only one row of data
    GridViewRow gvr = (GridViewRow)e.Row;
    if (gvr != null)
    {
        gvr.Visible = true;

        //...but hide page number if there is only one page
        if (gv.PageCount == 1)
        {
            gv.ShowFooter = true;
            gv.PagerSettings.Visible = false;
        }
    }
}

But it actually hides the entire Pager row! No! I want to hide only the page numbers.

The ShowFooter, is so it looks like the gridview is a box, closed off. But it's still ugly. I'd rather not use it if I can just keep the Pager row showing and being able to hide anything in it. ie.. keep background color intact.

Any other ideas? Thanks


Solution

  • Try something like

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
        if (e.Row.RowType == DataControlRowType.Pager && GridView1.PageCount==1 )
        { e.Row.Style.Add("color", "white"); }
     }
    

    Or, if you don't want to deal with colors you can try this (but I think it's less robust)

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    
        if (e.Row.RowType == DataControlRowType.Pager && GridView1.PageCount==1 )
        {
            var a = e.Row.Controls;
            if (a.Count>0 && a[0] is TableCell)
            {
                var b = a[0].Controls[0].Controls[0] as TableRow;
                if (b != null)
                {
                    //This is actually your page 1 text
                    b.Cells[0].Text = "";
                }
             }
         }
    }