Introduction
GridView is one of the most common tools for displaying data in a grid format in Asp.Net. When the data becomes large, paging helps the users to view chunks of data and also increases Page load time. In this post, we will see how can we implement Paging in a GridView control.
So, lets dig directly into the code.
ASPX Page
- In your Visual Studio solution, on any page drag and drop a GridView control.Lets name it as grdData.
- Set the AllowPaging property of the GridView as true.
- Handle the OnPageIndexChanging event of the GridView.
- By default GridView displays 10 records per page. If you want to modify the number of records displayed per page, set the PageSize property of GridView to your desired number. In this example, I have set this property to 5.
- Below is the complete GridView code in aspx page.
- <asp:gridview ID="grdData" runat="server" AutoGenerateColumns="False" CellPadding="4" PageSize="5"
- ForeColor="#333333" GridLines="None" Width="200" AllowPaging="True"
- OnPageIndexChanging="grdData_PageIndexChanging">
- <alternatingrowstyle BackColor="White" ForeColor="#284775"></alternatingrowstyle>
- <columns>
- <asp:boundfield DataField="ID" HeaderText="ID"></asp:boundfield>
- <asp:boundfield DataField="Name" HeaderText="Name"></asp:boundfield>
- </columns>
- <editrowstyle BackColor="#999999"></editrowstyle>
- <footerstyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></footerstyle>
- <headerstyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></headerstyle>
- <pagerstyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"></pagerstyle>
- <rowstyle BackColor="#F7F6F3" ForeColor="#333333"></rowstyle>
- <selectedrowstyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></selectedrowstyle>
- <sortedascendingcellstyle BackColor="#E9E7E2"></sortedascendingcellstyle>
- <sortedascendingheaderstyle BackColor="#506C8C"></sortedascendingheaderstyle>
- <sorteddescendingcellstyle BackColor="#FFFDF8"></sorteddescendingcellstyle>
- <sorteddescendingheaderstyle BackColor="#6F8DAE"></sorteddescendingheaderstyle>
- </asp:gridview>

Join the conversation! Your thoughts help the community grow.