In this article we will find out how to set paging in Grid View control in asp.net. In some situation we bind the Grid View to table that contain very large amount of data, so page size become very large to avoid this we can use paging in Grid View.
In this example I am using Visual Studio 2012 but this example is applicable to other version of Visual Studio as well.
To set paging two properties need to be set for Grid View Control:
1. AllowPaging equals to True.
2. Page Size (Number of record to be shown in one page)
Step 1: First we will create sample table in database . Run this script in Sql Server. This script will create new table with the name STUDENT_RECORD
CREATE TABLE [dbo].[STUDENT_RECORD](
[STU_ID] [int] IDENTITY(1,1)NOTNULL,
[STU_NAME] [varchar](max)NULL,
[STU_FATHER_NAME] [varchar](50)NULL,
[STU_ROLL_NO] [numeric](18, 0)NULL,
CONSTRAINT[PK_STUDENT_RECORD] PRIMARYKEYCLUSTERED
(
[STU_ID] ASC
)WITH (PAD_INDEX=OFF,STATISTICS_NORECOMPUTE=OFF,IGNORE_DUP_KEY=OFF,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)ON [PRIMARY]
)ON [PRIMARY]
GO
SETANSI_PADDINGOFF
GO
SETIDENTITY_INSERT [dbo].[STUDENT_RECORD] ON
INSERT [dbo].[STUDENT_RECORD]([STU_ID], [STU_NAME],[STU_FATHER_NAME], [STU_ROLL_NO])VALUES (1,N'Rohan Sharma',N'Ram Lal',CAST(383 ASNumeric(18, 0)))
INSERT [dbo].[STUDENT_RECORD]([STU_ID], [STU_NAME],[STU_FATHER_NAME], [STU_ROLL_NO])VALUES (2,N'Gautam Kapoor ',N'Sohan Kapoor',CAST(783 ASNumeric(18, 0)))
INSERT [dbo].[STUDENT_RECORD]([STU_ID], [STU_NAME],[STU_FATHER_NAME], [STU_ROLL_NO])VALUES (3,N'Deepak',N'Chetan',CAST(455 ASNumeric(18, 0)))
INSERT [dbo].[STUDENT_RECORD]([STU_ID], [STU_NAME],[STU_FATHER_NAME], [STU_ROLL_NO])VALUES (4,N'Gaurav',N'Dr. KP Singh',CAST(345 ASNumeric(18, 0)))
INSERT [dbo].[STUDENT_RECORD]([STU_ID], [STU_NAME],[STU_FATHER_NAME], [STU_ROLL_NO])VALUES (5,N'Ravi',N'BK Kumar',CAST(888 ASNumeric(18, 0)))
INSERT [dbo].[STUDENT_RECORD]([STU_ID], [STU_NAME],[STU_FATHER_NAME], [STU_ROLL_NO])VALUES (6,N'Sonam',N'DK Sharma',CAST(999 ASNumeric(18, 0)))
INSERT [dbo].[STUDENT_RECORD]([STU_ID], [STU_NAME],[STU_FATHER_NAME], [STU_ROLL_NO])VALUES (7,N'Anamika',N'VP Singh',CAST(777 ASNumeric(18, 0)))
INSERT [dbo].[STUDENT_RECORD]([STU_ID], [STU_NAME],[STU_FATHER_NAME], [STU_ROLL_NO])VALUES (8,N'Shailaja',N'DP Mian',CAST(999 ASNumeric(18, 0)))
SETIDENTITY_INSERT [dbo].[STUDENT_RECORD] OFF
Step 2: Now in visual studio Add a new web form . Set connection string in web.config file. check this article to read connection string from web.cofig file: Connection String in web.config file.
Step 3: In the web form add this code given below. Note that we have set AllowPaging Property to True and PageSize=5. The gridview OnPageIndexChanging event handle the code of Paging in Grid View control.
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="grdstudent"runat="server"AllowPaging="true"PageSize="5"DataKeyNames="STU_ID"AutoGenerateColumns="False"BackColor="White"BorderColor="#DEDFDE"BorderStyle="None"BorderWidth="1px"CellPadding="4"ForeColor="Black"GridLines="Vertical"Height="273px"Width="460px"OnPageIndexChanging="grdstudent_PageIndexChanging">
<AlternatingRowStyleBackColor="White"/>
<Columns>
<asp:BoundFieldDataField="STU_NAME"HeaderText="Student Name"SortExpression="STU_NAME"/>
<asp:BoundFieldDataField="STU_FATHER_NAME"HeaderText="Father Name"SortExpression="STU_FATHER_NAME"/>
<asp:BoundFieldDataField="STU_ROLL_NO"HeaderText="Roll No"SortExpression="STU_ROLL_NO"/>
</Columns>
<FooterStyleBackColor="#CCCC99"/>
<HeaderStyleBackColor="#6B696B"Font-Bold="True"ForeColor="White"/>
<PagerStyleBackColor="#F7F7DE"ForeColor="Black"HorizontalAlign="center"/>
<RowStyleBackColor="#F7F7DE"/>
<SelectedRowStyleBackColor="#CE5D5A"Font-Bold="True"ForeColor="White"/>
<SortedAscendingCellStyleBackColor="#FBFBF2"/>
<SortedAscendingHeaderStyleBackColor="#848384"/>
<SortedDescendingCellStyleBackColor="#EAEAD3"/>
<SortedDescendingHeaderStyleBackColor="#575357"/>
</asp:GridView>
</div>
</form>
</body>
</html>
Step 4: In the code behind file add the following code:
C# Code for Paging in Grid View:
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Bind_Grid();
}
}
protectedvoid Bind_Grid()
{
SqlConnection con=newSqlConnection(ConfigurationManager.ConnectionStrings["dbcon"].ToString());
con.Open();
SqlDataAdapter adp = newSqlDataAdapter("Select * From Student_Record", con);
DataTable dt = newDataTable();
adp.Fill(dt);
grdstudent.DataSource = dt;
grdstudent.DataBind();
con.Close();
}
protectedvoid grdstudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdstudent.PageIndex = e.NewPageIndex;
Bind_Grid();
}