Pagination is useful technique in case of large data. When data is
large , it’s not possible to show whole data in gridview. It is not user-friendly.
User has to scroll to see a particular record. Paging is helpful in this case. We
can show particular no of record in single page. By default gridview show “10” records per page.
In this article I will explain how to implement Paging in
gridview.
1.
Create a webpage and add a gridview control on webpage.
2.
Now Set “AllowPaging”
property to true. Now click on “PageindexChanging”
event of gridview.
3.
Now Bind gridview with data from database and write code on Page “GridView1_PageIndexChanging” event to handle pagination.
Please check my previous article to bind gridview “How
to Bind Gridview with database?”
Implementation:
Customize gridview as given below
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" Width="270px" onpageindexchanging="GridView1_PageIndexChanging" AllowPaging="True"
PageSize="3">
<Columns>
<asp:BoundField DataField="Student_Name" HeaderText="Student name" />
<asp:BoundField DataField="age" HeaderText="Age" />
<asp:BoundField DataField="class" HeaderText="Class" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB"
/>
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E"
/>
</asp:GridView>
|
Asp.NET code to bind grid and Pagination:
C#: Add following Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
|
VB.NET:
Add
following namespaces
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
|
C#
Code to Bind Grid & Paging
public void
Fill_Grid()
{
if
(con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter dataadapater = new
SqlDataAdapter("Select
* from student_info", con);
dataadapater.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected
void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs
e)
{
GridView1.PageIndex = e.NewPageIndex;
Fill_Grid();
}
|
VB.NET
Code to Bind Grid & Paging
Public Sub
Fill_Grid()
If
con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
Dim ds As New DataSet()
Dim dataadapater As New SqlDataAdapter("Select
* from student_info", con)
dataadapater.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
End Sub
Protected Sub
GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
Fill_Grid()
End Sub
|
Use the above code and check result.
Note:
You can ask if you have doubt in article or other article by posting in comment
area.