Paging in DataGrid

This is application logic this code demonstrates access to the access Database. The result sets are stored in the DataSet and it is assigned to the Datagrid for Datasource.
 
The result set data's are viewed as per the paging properties which is in HTML view (presentation logic) which is given later.
  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Web;  
  7. using System.Web.SessionState;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Data.OleDb;  
  12.   
  13. namespace Sample {  
  14.   /// <summary>  
  15.   /// Summary description for WebForm1.  
  16.   /// </summary>  
  17.   public class WebForm1: System.Web.UI.Page {  
  18.     protected System.Web.UI.WebControls.Label Label1;  
  19.     protected System.Web.UI.WebControls.DataGrid DataGrid1;  
  20.     protected System.Web.UI.WebControls.TextBox TextBox1;  
  21.     public DataSet ds = new DataSet();  
  22.     public OleDbDataAdapter odap;  
  23.     public OleDbConnection con;  
  24.   
  25.     private void Page_Load(object sender, System.EventArgs e) {  
  26.       // Put user code to initialize the page here  
  27.   
  28.       if (!this.IsPostBack) {  
  29.         con = new OleDbConnection(@ "Provider=Microsoft.JET.OLEDB.4.0;data source=c:\robert\emp.mdb");  
  30.         odap = new OleDbDataAdapter("select * from emp", con);  
  31.         odap.Fill(ds, "emp");  
  32.         DataGrid1.DataSource = ds;  
  33.         DataGrid1.DataBind();  
  34.         Label1.Text = "Enter the Employee Name";  
  35.       }  
  36.     }  
  37.  
  38.     # region Web Form Designer generated code  
  39.     override protected void OnInit(EventArgs e) {  
  40.       //  
  41.       // CODEGEN: This call is required by the ASP.NET Web Form Designer.  
  42.       //  
  43.       InitializeComponent();  
  44.       base.OnInit(e);  
  45.     }  
  46.   
  47.     /// <summary>  
  48.     /// Required method for Designer support - do not modify  
  49.     /// the contents of this method with the code editor.  
  50.     /// </summary>  
  51.     private void InitializeComponent() {  
  52.       this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);  
  53.       this.Load += new System.EventHandler(this.Page_Load);  
  54.   
  55.     }#  
  56.     endregion  
  57.   
  58.     private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) {  
  59.       DataGrid1.CurrentPageIndex = e.NewPageIndex;  
  60.   
  61.       BindData();  
  62.     }  
  63.     public void BindData() {  
  64.       con = new OleDbConnection(@ "Provider=Microsoft.JET.OLEDB.4.0;data source=c:\robert\emp.mdb");  
  65.       odap = new OleDbDataAdapter("select * from emp", con);  
  66.       odap.Fill(ds, "emp");  
  67.       DataGrid1.DataSource = ds;  
  68.       DataGrid1.DataBind();  
  69.     }  
  70.   }  
  71. }  
This part presents the presentation logic.
 
Paging properties are set in the Datagrid Web Control.
  1. <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Sample.WebForm1" %>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >  
  3. <HTML>  
  4.   
  5.   <HEAD>  
  6.     <title>WebForm1</title>  
  7.     <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">  
  8.     <meta name="CODE_LANGUAGE" Content="C#">  
  9.     <meta name="vs_defaultClientScript" content="JavaScript">  
  10.     <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">  
  11.     <LINK href="../Sample/Styles.css" type="text/css" rel="stylesheet">  
  12.   </HEAD>  
  13.   
  14.   <body MS_POSITIONING="GridLayout">  
  15.     <form id="Form1" method="post" runat="server">  
  16.       <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 304px; POSITION: absolute; TOP: 112px" runat="server" Width="184px" Height="32px" CssClass="txtBox"></asp:TextBox>  
  17.       <asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 104px" runat="server" Height="40px" Width="184px" CssClass="lab"></asp:Label>  
  18.       <asp:DataGrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 312px; POSITION: absolute; TOP: 224px" runat="server" Height="200px" Width="512px" AutoGenerateColumns="False" AllowPaging="True" PageSize="2" CssClass="pagLinks">  
  19.         <PagerStyle Font-Italic="True" Wrap="True" Mode="NumericPages"></PagerStyle>  
  20.         <Columns>  
  21.           <asp:BoundColumn DataField="empName" HeaderText="EmployeeName"></asp:BoundColumn>  
  22.           <asp:BoundColumn DataField="empID" HeaderText="EmployeeID"></asp:BoundColumn>  
  23.           <asp:BoundColumn DataField="empsal" HeaderText="EmployeeSalary"></asp:BoundColumn>  
  24.         </Columns>  
  25.       </asp:DataGrid>  
  26.     </form>  
  27.   </body>  
  28.   
  29. </HTML>  


Similar Articles