Paging in asp.net
gridview:
Question: How to
bind gridview in asp.net and apply paging.
Answer: Here is an
example of how to bind gridview.
Create a website and add a page. Suppose there is Default.aspx
and its code behind page is Default.aspx.cs
In Default.aspx page add a gridview control and suppose its
id is “grdEmployeeList”.
See below code:
<%@ Page
Language="C#"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//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">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdEmployeeList"
runat="server"
BackColor="White"
AutoGenerateColumns="False"
BorderColor="#999999"
BorderStyle="None"
BorderWidth="1px"
CellPadding="3"
AllowPaging="True"
PageSize="5"
EnableModelValidation="True"
GridLines="Vertical"
PagerSettings-Position="Bottom"
onpageindexchanging="grdEmployeeList_PageIndexChanging">
<AlternatingRowStyle
BackColor="#DCDCDC"
/>
<Columns>
<asp:BoundField DataField="EMP_ID"
HeaderText="Employee
ID" />
<asp:BoundField DataField="EMP_NAME"
HeaderText="Employee
Name" />
<asp:BoundField DataField="DEPARTMENT_NAME"
HeaderText="Department
Name" />
<asp:BoundField DataField="SALARY"
HeaderText="Salary"
/>
<asp:BoundField DataField="JOINING_DATE"
HeaderText="Joining
Date" />
</Columns>
<FooterStyle BackColor="#CCCCCC"
ForeColor="Black"
/>
<HeaderStyle BackColor="#000084"
Font-Bold="True"
ForeColor="White"
/>
<PagerStyle BackColor="#999999"
ForeColor="Black"
HorizontalAlign="Center"
/>
<RowStyle BackColor="#EEEEEE"
ForeColor="Black"
/>
<SelectedRowStyle
BackColor="#008A8C"
Font-Bold="True"
ForeColor="White"
/>
</asp:GridView>
</div>
</form>
</body>
</html>
Here paging will be shown by AllowPaging="True"
and PageSize="5". Default pagesize is 10.
You can change page size whatever you want. Onpageindexchanging event
is used for applying paging the gridview.
In Default.aspx.cs page write following page.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
SqlConnection conn = new
SqlConnection(@"Data
Source=SHASHI-PC\SQLSERVER2008;Initial Catalog=TestDB;User
ID=testdb_user;Password=testdb_pass");
//change connection string as per your choice like Data
Source , Database name (Initial Catalog), User ID and password of database.
protected void
Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindEmplyeeList();//bind gridview on pageload
for employee list
}
}
protected void
grdEmployeeList_PageIndexChanging(object
sender, GridViewPageEventArgs e)
{
if (e.NewPageIndex >= 0)
{
grdEmployeeList.PageIndex = e.NewPageIndex;//change
pageindex of the grid
BindEmplyeeList();
}
}
//bind gridview from database
private void
BindEmplyeeList()
{
SqlDataAdapter sda = new
SqlDataAdapter("Select
EMP_ID , EMP_NAME , DEPARTMENT_NAME, SALARY, JOINING_DATE From
MST_EMPLOYEE_MASTER", conn);
DataTable dtEmp = new
DataTable();
sda.Fill(dtEmp);
grdEmployeeList.DataSource = dtEmp;
grdEmployeeList.DataBind();
}
}
Result:
=================================================================
Hope you have enjoyed with this article. Please
write your comments for it.
=================================================================