This article shows how to do paging in a GridView in ASP.NET using C#, where we take one GridView and SQL Server. We insert a large amount of data into the table to show how the paging works.
INITIAL CHAMBER
Step 1
Open your Visual Studio 2010 and create an empty website, provide a suitable name such as gridview_paging_demo.
Step 2
In Solution Explorer you get your empty website, then add web forms and a SQL Server database as in the following.
For Web Form
gridview_paging_demo (your empty website). Right-click and select Add New Item -> Web Form. Name it gridview_paging_demo.aspx.
For SQL Server database
gridview_paging_demo (your empty website). Right-click and select Add New Item -> SQL Server database. Add a database inside the App_Data_folder.
DATABASE CHAMBER
Step 3
In Server Explorer, click on your database (Database.mdf), then select Tables -> Add New Table. Form the table like the following:
Table tbl_data (Don't forget to make the ID as IS Identity -- True)
Figure 1: Tbl_Data
DESIGN CHAMBER
Step 4
Open the gridview_paging_demo.aspx file and write some code for the design of the application.
- <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <headrunatheadrunat="server">
- <title></title>
- <styletypestyletype="text/css">
- .style1
- {
- text-decoration: underline;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <formidformid="form1"runat="server">
- <div>
- <tablestyletablestyle="width:100%;">
- <tr>
- <td>
- </td>
- <tdclasstdclass="style1">
- <strong>GridView Paging Example</strong>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <asp:GridViewIDasp:GridViewID="GridView1"runat="server"AllowPaging="True"
- AutoGenerateColumns="False"BackColor="White"BorderColor="#E7E7FF"
- BorderStyle="None"BorderWidth="1px"CellPadding="3"DataKeyNames="id"
- GridLines="Horizontal"onpageindexchanging="GridView1_PageIndexChanging"
- PageSize="4"style="margin-left: 239px">
- <AlternatingRowStyleBackColorAlternatingRowStyleBackColor="#F7F7F7"/>
- <Columns>
- <asp:TemplateFieldHeaderTextasp:TemplateFieldHeaderText="Student ID">
- <EditItemTemplate>
- <asp:TextBoxIDasp:TextBoxID="TextBox1"runat="server"Text='<%# Bind("id") %>'>
- </asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:LabelIDasp:LabelID="Label1"runat="server"Text='<%# Bind("id") %>'>
- </asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateFieldHeaderTextasp:TemplateFieldHeaderText="Student Name">
- <EditItemTemplate>
- <asp:TextBoxIDasp:TextBoxID="TextBox2"runat="server"Text='<%# Bind("name") %>'>
- </asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:LabelIDasp:LabelID="Label2"runat="server"Text='<%# Bind("name") %>'>
- </asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateFieldHeaderTextasp:TemplateFieldHeaderText="Address">
- <EditItemTemplate>
- <asp:TextBoxIDasp:TextBoxID="TextBox3"runat="server"Text='<%# Bind("city") %>'>
- </asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:LabelIDasp:LabelID="Label3"runat="server"Text='<%# Bind("city") %>'>
- </asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- <FooterStyleBackColorFooterStyleBackColor="#B5C7DE"ForeColor="#4A3C8C"/>
- <HeaderStyleBackColorHeaderStyleBackColor="#4A3C8C"Font-Bold="True"ForeColor="#F7F7F7"/>
- <PagerStyleBackColorPagerStyleBackColor="#E7E7FF"ForeColor="#4A3C8C"HorizontalAlign="Right"/>
- <RowStyleBackColorRowStyleBackColor="#E7E7FF"ForeColor="#4A3C8C"/>
- <SelectedRowStyleBackColorSelectedRowStyleBackColor="#738A9C"Font-Bold="True"ForeColor="#F7F7F7"/>
- <SortedAscendingCellStyleBackColorSortedAscendingCellStyleBackColor="#F4F4FD"/>
- <SortedAscendingHeaderStyleBackColorSortedAscendingHeaderStyleBackColor="#5A4C9D"/>
- <SortedDescendingCellStyleBackColorSortedDescendingCellStyleBackColor="#D8D8F0"/>
- <SortedDescendingHeaderStyleBackColorSortedDescendingHeaderStyleBackColor="#3E3277"/>
- </asp:GridView>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
This is the GridView design and the properties window where you need to include the even–page changing index and page size = 4.
Figure 2: Properties
Figure 3: GridView Paging
CODE CHAMBER
Step 5
In the code chamber we will write some code so that our application works.
gridview_paging_demo.aspx.cs
- using System;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Web;
- usingSystem.Web.UI;
- usingSystem.Web.UI.WebControls;
- usingSystem.Data;
- usingSystem.Data.SqlClient;
- publicpartialclass_Default: System.Web.UI.Page
- {
- protectedvoidPage_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- refreshdata();
- }
- }
- publicvoidrefreshdata()
- {
- SqlConnection con = newSqlConnection(@
- "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommandcmd = newSqlCommand("select * from tbl_data", con);
- SqlDataAdaptersda = newSqlDataAdapter(cmd);
- DataTabledt = newDataTable();
- sda.Fill(dt);
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
- protectedvoid GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- GridView1.PageIndex = e.NewPageIndex;
- refreshdata();
- }
- }

Figure 4: Output 1

Figure 5: Output 2
I hope you liked this. Have a good day. Thank you for reading.

Arul RPosted Oct 28, 2015, 5:38 AM
Good one !!!
Santhakumar MunuswamyPosted Jul 11, 2015, 2:59 AM
Nice article! Thanks for sharing
Dilini WanasekaraPosted Jul 10, 2015, 10:23 PM
Nice article
Debasis SahaPosted Jul 10, 2015, 4:57 AM
Good One..
Sibeesh VenuPosted Jul 10, 2015, 4:51 AM
God one.
RakeshPosted Jul 10, 2015, 2:18 AM
Good Explain
Pankaj Kumar ChoudharyPosted Jul 9, 2015, 8:24 PM
A Great series of Article Sir...... Keep it up..........