How to add paging in DataGridView of ASP.Net



In general, we often use paging feature in gridview control for all projects.  Suppose if you want to display large amount records in gridview, we use paging and display set of few records in each gridview pages.All of us who have experience using GridView in ASP.NET know paging is an easy job using GridView control in ASP.NET. Paging is an important thing for thousands of thousand data if we want to develop a scalable application. In this article i will tell you How to add paging in DataGridView of ASP Net.


In the Previous articles i explained how to send value from one form to another using c#.netallow only alpha numeric values in a textbox using asp.nethow to create cms website using c# and asp.nethow to sell products online through my website, how to create setup of visual studio windows application with database and many more about ASP.Net.

Step 1.

Set "AllowPaging" Property to true.

Step 2.

Set "Page Size" property to 10. So that GridView shows 10 records per page.

Step 3.

Write following code on "PageIndexChanging" event of GridView.

DataGridView1.PageIndex = e.NewPageIndex;

You are Done.

Step 4.

Bind Data with DataGridView.

Add Connection String between <configuration> tab of web.config file. And replace Data Source with your PC Name and also Catalog with your database name.

    <add name="database_con" connectionString="Data Source=Your-PC\SQLEXPRESS;Initial Catalog=YourDatabasename;Integrated Security=True" providerName="System.Data.SqlClient"/>

Add Data Source

<asp:SqlDataSource ID="myDataSource" runat="server" 
    ConnectionString = "<%$ ConnectionStrings:database_con %>" 
     SelectCommand="SELECT * FROM table1" >

Your Grid View will look like this.

 <asp:GridView ID="DataGridView1" DataSourceID="myDataSource" runat="server"
 AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" 
PageSize="10" onpageindexchanging="DataGridView1_PageIndexChanging"  HeaderStyle-Font-Names="Verdana" Font-Size="Small" 
HeaderStyle-Font-Underline="false" Width="55%" 
HeaderStyle-BackColor="BurlyWood" HeaderStyle-ForeColor="Navy">
            <alternatingrowstyle backcolor="</span>Aquamarine"> />
   <asp:BoundField  DataField="ProfileId" HeaderText="Profile Id" 
    SortExpression="ProfileId" ItemStyle-Width="6%"/>
   <asp:BoundField  DataField="Name" HeaderText="Name" 
    SortExpression="Name" ItemStyle-Width="13%"/>
   <asp:BoundField  DataField="Address" HeaderText="Address" 
    SortExpression="Address" ItemStyle-Width="18%"/>
   <asp:BoundField  DataField="Email" HeaderText="Email" 
    SortExpression="Email" ItemStyle-Width="8%"/>
   <asp:BoundField  DataField="Mobile" HeaderText="Mobile" 
    SortExpression="Mobile" ItemStyle-Width="9%"/>
   <asp:BoundField  DataField="IsActive" HeaderText="Status" 
    SortExpression="IsActive" ItemStyle-Width="4%"/>

Step 5.

Run Your Application.


Give your Valuable Comments.
comments powered by Disqus
Follow me on twitter
About Me
Harpreet Singh
I began programming with C++ when i was 17. Then at the middle of my study .NET came. Then I began to read C# and VB.NET. By the time i learnt SQL and ASP.NET, and developed some websites such as news portals that are active now. Currently i am running my own Software company.
More Articles