custom paging on gridview control

Nick Kahn picture Nick Kahn · Aug 9, 2010 · Viewed 10.9k times · Source

below code works fine if i remove the PagerSetting or remove PagerTemplate so if i have both (PagerSetting & PagerTemplate) then my page number is not display.

my qeustion is: how can i display both (PagerTemplate and PagerSetting) togather at the bottom of the Gridview ? plese see the below source code.

<asp:GridView ID="gvTable" runat="server" ShowHeader="true"     
  PageSize="5" AllowPaging="true" AllowSorting="true"     
  DataSourceID="myLinqDataSource" AutoGenerateColumns="false"     
  OnRowDataBound="GridView_DataBound">     
  <Columns>     
    <asp:BoundField DataField="Edited" HeaderText="Date" DataFormatString="{0:d}" />     
    <asp:BoundField DataField="Activity" HeaderText="Notes" />     
  </Columns>     
<PagerStyle CssClass="pager-row" />    
                    <RowStyle CssClass="row" />    
                    <PagerSettings Mode="NumericFirstLast" PageButtonCount="7" FirstPageText="«" LastPageText="»" />    
                   **<PagerTemplate>**     
                        <div style="float: left; margin-left: 12px;">    
                            <div style="float: left; margin: 4px 6px 0px 0px;">Page Size</div>    
                            <asp:DropDownList ID="ddlPageSizeChange" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageSizeChange">    
                                <asp:ListItem>15</asp:ListItem>    
                                <asp:ListItem>25</asp:ListItem>    
                                <asp:ListItem>50</asp:ListItem>    
                                <asp:ListItem>100</asp:ListItem>    
                            </asp:DropDownList>    
                        </div>    
                        <div class="gridCount" runat="server" id="divGridCount"><b>1</b> Items Found  </div>    
                    </PagerTemplate>      
</asp:GridView>  

UPDATE 1:

I able to display paging 1 2 3 4 5... but the problem is: i can not have both PagerSetting & PagerTemplate and if i have both(PagerSetting & PagerTemplate) in my gridview my paging (1 2 3 4 5) is not displaying and if i remove PagerTemplate than my paging is displaying (1 2 3 4 5...) make sense?

UPDATE:

Here is what i am trying to get:

<< < 1 2 3 4 5 ..... > >> Total Pages Found 80 - Page 1/80 - PageSize {15,25,50,10} (this will be a dropdownlist)

Answer

Gajanan chavhan picture Gajanan chavhan · Sep 25, 2015

you can do this by using follow code

  1. Back end code ( row created event of gridview):

    protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.Pager)
    {
        TableRow tr = (TableRow)e.Row.Cells[0].Controls[0].Controls[0];
        if (tr.Cells[1] != null && (((tr.Cells[1]).Controls[0]) is LinkButton))
        {
            LinkButton btnPrev = (LinkButton)(tr.Cells[1]).Controls[0];
            if (btnPrev.Text == "...")
            {
                (((tr.Cells[1]).Controls[0]) as LinkButton).Text = "<";
            }
        }
        if (tr.Cells[tr.Cells.Count - 2] != null && (((tr.Cells[tr.Cells.Count - 2]).Controls[0]) is LinkButton))
        {
            LinkButton btnNext = (LinkButton)(tr.Cells[tr.Cells.Count - 2]).Controls[0];
            if (btnNext.Text == "...")
            {
                (((tr.Cells[tr.Cells.Count - 2]).Controls[0]) as LinkButton).Text = ">";
            }
        }
    }
    

    }

  2. and use pagersetting as:

    <PagerSettings  Mode="NumericFirstLast" FirstPageText="<<"
    LastPageText=">>" />
    

you will get your output. :)

Note: don't forgot to set pageSize and AllowPaging ="true" of grid.