Introduction:
Paging
in gridview is very important concept in case of large data. If data is large,
it is not possible to show data in single page. So in that case we will use
paging to break data in no of pages. Pageindexchanging event of gridview is used for paging.
In this article, I will explain
how to implement paging in gridview in asp.net using pageindexchanging event with example using c#, vb.net .
Implementation: Let's create a demo
website to demonstrate the concept.
Create a database “Test”, Then
create one table i.e. Emp_Personal
Column
Name
|
Data
Type
|
EmpPer_Id
|
Int(Primary Key. So set is
identity=true)
|
EmpName
|
varchar(100)
|
Age
|
Int
|
Address
|
varchar(500)
|
Source Code: Sample Application
Design Page: design your page as given below
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Bind data to gridview in asp.net
using c#, vb.net</title>
</head>
<body>
<form id="form1"
runat="server">
<fieldset style="width:300px">
<legend><strong>Paging in
Gridview</strong></legend>
<div class="GridviewDiv">
<asp:GridView runat="server"
ID="gvDetails"
AllowPaging="true"
PageSize="5"
AutoGenerateColumns="false"
Width="350px"
OnPageIndexChanging="gvDetails_PageIndexChanging">
<HeaderStyle BackColor="#620303"
ForeColor="#ffffff"
/>
<Columns>
<asp:BoundField DataField="EmpPer_Id"
HeaderText="Employee
Id" />
<asp:BoundField DataField="EmpName"
HeaderText="Employee
Name" />
<asp:BoundField DataField="Age"
HeaderText="Age"
/>
</Columns>
</asp:GridView>
</div></fieldset>
</form>
</body>
</html>
ASP.NET Code behind File(c#):
Write following code in code behind file:
Write following code in code behind file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
public partial
class paging
: System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
DataSet
ds = new DataSet();
using (SqlConnection con = new
SqlConnection("Data
Source=localhost;Integrated Security=true;Initial Catalog=Test"))
{
con.Open();
SqlCommand
cmd = new SqlCommand("select * from Emp_Personal", con);
SqlDataAdapter
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
}
protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs
e)
{
gvDetails.PageIndex = e.NewPageIndex;
BindGridview();
}
}
ASP.NET Code behind File (VB.NET):
Write following code in code behind file:
Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI.WebControls
Partial Public
Class paging
Inherits
System.Web.UI.Page
Protected Sub Page_Load(ByVal
sender As Object,
ByVal e As EventArgs)
If Not IsPostBack Then
BindGridview()
End If
End Sub
Protected Sub BindGridview()
Dim ds As DataSet = New DataSet()
Imports
(SqlConnection con = New
SqlConnection("Data Source=localhostIntegrated Security=TrueInitial
Catalog=Test"))
{
con.Open()
Dim cmd
As SqlCommand
= New SqlCommand("select * from Emp_Personal", con)
Dim da As SqlDataAdapter
= New SqlDataAdapter(cmd)
da.Fill(ds)
con.Close()
gvDetails.DataSource = ds
gvDetails.DataBind()
}
End Sub
Protected Sub gvDetails_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvDetails.PageIndex = e.NewPageIndex
BindGridview()
End Sub
End Class
0 comments:
Post a Comment