How to Implement Paging in GridView Control in ASP.Net

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 article, we will see how to implement paging in a GridView control.

So, let's dig directly into the code.

ASPX Page

  • In your Visual Studio solution, on any page drag and drop a GridView control. Let's name it grdData.
  • Set the AllowPaging property of the GridView to 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 the GridView to your desired number. In this example, I have set this property to 5.
  • The following is the complete GridView code in the aspx page.
  1. <asp:gridview ID="grdData" runat="server" AutoGenerateColumns="False" CellPadding="4" PageSize="5"    
  2.         ForeColor="#333333" GridLines="None" Width="200" AllowPaging="True"    
  3.         OnPageIndexChanging="grdData_PageIndexChanging">    
  4.         <alternatingrowstyle BackColor="White" ForeColor="#284775"></alternatingrowstyle>    
  5.           <columns>    
  6.              <asp:boundfield DataField="ID" HeaderText="ID"></asp:boundfield>    
  7.              <asp:boundfield DataField="Name" HeaderText="Name"></asp:boundfield>    
  8.           </columns>    
  9.            <editrowstyle BackColor="#999999"></editrowstyle>    
  10.            <footerstyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></footerstyle>    
  11.            <headerstyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></headerstyle>    
  12.            <pagerstyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"></pagerstyle>    
  13.            <rowstyle BackColor="#F7F6F3" ForeColor="#333333"></rowstyle>    
  14.            <selectedrowstyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></selectedrowstyle>    
  15.            <sortedascendingcellstyle BackColor="#E9E7E2"></sortedascendingcellstyle>    
  16.            <sortedascendingheaderstyle BackColor="#506C8C"></sortedascendingheaderstyle>    
  17.            <sorteddescendingcellstyle BackColor="#FFFDF8"></sorteddescendingcellstyle>    
  18.            <sorteddescendingheaderstyle BackColor="#6F8DAE"></sorteddescendingheaderstyle>    
  19. </asp:gridview>    
ASPX.CS Page
  • Create a new function that loads data from your data repository (database) and bind it to the GridView. Let's name it LoadGridData().
  • On the Page_Load event, call the LoadGridData() after checking the IsPostback property of the page.
  • In the grdData_PageIndexChanging event, write the following code. In the following code, we are setting the NewPageIndex property of the GridView and calling the LoadGridData() fucntion again. The .Net GridView automatically handles the internal stuff for displaying only the data for the selected page.
  • The following is the complete code from the .aspx.cs page: 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.    if (!IsPostBack)  
  4.    LoadGridData();  
  5. }  
  6. private void LoadGridData()  
  7. {  
  8.      //I am adding dummy data here. You should bring data from your repository.  
  9.      DataTable dt = new DataTable();  
  10.      dt.Columns.Add("Id");  
  11.      dt.Columns.Add("Name");  
  12.      for (int i = 0; i < 10; i++)  
  13.      {  
  14.            DataRow dr = dt.NewRow();  
  15.            dr["Id"] = i + 1;  
  16.            dr["Name"] = "Student " + (i + 1);  
  17.            dt.Rows.Add(dr);  
  18.      }  
  19.      grdData.DataSource = dt;  
  20.      grdData.DataBind();  
  21. }  
  22. protected void grdData_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  23. {  
  24.      grdData.PageIndex = e.NewPageIndex;  
  25.      LoadGridData();  
  26. }  
You're done.
 
I hope you like this! Keep learning and sharing! Cheers!


Similar Articles
Rebin Infotech
Think. Innovate. Grow.