When working with a large number of database rows, it is useful to display the rows in
different pages. we can enable paging with the GridView control by enabling its AllowPaging property.
and set PageSize what no of row what to display on page.
Code: Ex02DataPaging.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Ex02DataPaging.aspx.cs"
Inherits="LearnAsp.Net.ControlDemo.GridView.Ex02DataPaging" %>
<!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>
<h3>GridView Control Paging</h3>
<asp:GridView ID="grdDetails2" PageSize="#121212" BackColor="#d4d8ea"
ForeColor="#f526b5" runat="server" AutoGenerateColumns="false"
onpageindexchanging="grdDetails2_PageIndexChanging" AllowPaging="true" PageSize="5">
<Columns>
<asp:BoundField DataField="empno" HeaderText="Emp No" />
<asp:BoundField DataField="ename" HeaderText="Employee Name" />
<asp:BoundField DataField="dname" HeaderText="Department" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#969ba2" ForeColor="White" Font-Bold="true" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
</form>
</body>
</html>
Code: Ex02DataPaging.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using LearnAspClassLib.Method;
using System.Configuration;
using System.Data.OracleClient;
namespace LearnAsp.Net.ControlDemo.GridView
{
public partial class Ex02DataPaging : System.Web.UI.Page
{
static string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
private OracleConnection objConn = new OracleConnection(strConn);
protected void Page_Load(object sender, EventArgs e)
{
String query = " SELECT empno, ename, d.deptno, d.dname FROM emp e INNER JOIN dept d ON e.deptno = d.deptno ";
OracleCommand objCmd = new OracleCommand(query, objConn);
DataTable dt = new DataTable();
objConn.Open();
OracleDataAdapter oda = new OracleDataAdapter(objCmd);
oda.Fill(dt);
objConn.Close();
ViewState["dt"] = dt;
grdDetails2.DataSource = dt;
grdDetails2.DataBind();
}
protected void grdDetails2_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdDetails2.PageIndex = e.NewPageIndex;
DataTable tb = (DataTable)ViewState["dt"];
if (tb.Rows.Count > 0)
{
grdDetails2.DataSource = tb;
grdDetails2.DataBind();
}
else
{
grdDetails2.DataSource = null;
grdDetails2.DataBind();
return;
}
}
}
}
Output:
No comments:
Post a Comment
Please do not enter any spam link in the comment box.