Paging in DataGrid


This is application logic this code demonstrates the 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.

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.OleDb;

 

namespace Sample

{

          /// <summary>

          /// Summary description for WebForm1.

          /// </summary>

          public class WebForm1 : System.Web.UI.Page

          {

                   protected System.Web.UI.WebControls.Label Label1;

                   protected System.Web.UI.WebControls.DataGrid DataGrid1;

                   protected System.Web.UI.WebControls.TextBox TextBox1;

                   public DataSet ds = new DataSet();

                   public OleDbDataAdapter odap;

                   public OleDbConnection con;

 

                   private void Page_Load(object sender, System.EventArgs e)

                   {

                             // Put user code to initialize the page here

 

                             if(!this.IsPostBack)

                             {

                                      con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;data source=c:\robert\emp.mdb");

                                      odap = new OleDbDataAdapter("select * from emp",con);

                                      odap.Fill(ds,"emp");

                                      DataGrid1.DataSource=ds;

                                      DataGrid1.DataBind();

                                      Label1.Text = "Enter the Employee Name";

                             }

                   }

 

                   #region Web Form Designer generated code

                   override protected void OnInit(EventArgs e)

                   {

                             //

                             // CODEGEN: This call is required by the ASP.NET Web Form Designer.

                             //

                             InitializeComponent();

                             base.OnInit(e);

                   }

 

                   /// <summary>

                   /// Required method for Designer support - do not modify

                   /// the contents of this method with the code editor.

                   /// </summary>

                   private void InitializeComponent()

                   {   

                             this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);

                             this.Load += new System.EventHandler(this.Page_Load);

 

                   }

                   #endregion

 

                   private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

                   {

                             DataGrid1.CurrentPageIndex = e.NewPageIndex;

       

                             BindData();

                   }

                   public void BindData()

                   {

                             con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;data source=c:\robert\emp.mdb");

                             odap = new OleDbDataAdapter("select * from emp",con);

                             odap.Fill(ds,"emp");

                             DataGrid1.DataSource = ds;

                             DataGrid1.DataBind();    

                   }

          }

}

This part presents the presentation logic.

Paging properties are set in the Datagrid Web Control.

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Sample.WebForm1" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

 <HEAD>

  <title>WebForm1</title>

  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

  <meta name="CODE_LANGUAGE" Content="C#">

  <meta name="vs_defaultClientScript" content="JavaScript">

  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

  <LINK href="../Sample/Styles.css" type="text/css" rel="stylesheet">

 </HEAD>

 <body MS_POSITIONING="GridLayout">

  <form id="Form1" method="post" runat="server">

   <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 304px; POSITION: absolute; TOP: 112px"

    runat="server" Width="184px" Height="32px" CssClass="txtBox"></asp:TextBox>

   <asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 104px" runat="server"

    Height="40px" Width="184px" CssClass="lab"></asp:Label>

   <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">

    <PagerStyle Font-Italic="True" Wrap="True" Mode="NumericPages"></PagerStyle>

    <Columns>

     <asp:BoundColumn DataField="empName" HeaderText="EmployeeName"></asp:BoundColumn>

     <asp:BoundColumn DataField="empID" HeaderText="EmployeeID"></asp:BoundColumn>

     <asp:BoundColumn DataField="empsal" HeaderText="EmployeeSalary"></asp:BoundColumn>

    </Columns>

   </asp:DataGrid>

  </form>

 </body>

</HTML>

Up Next
    Ebook Download
    View all
    Learn
    View all