Skip to main content

How To add Alphabet Paging in GridView

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AlphabetPaging.aspx.cs" Inherits="AlphabetPaging" %>

<!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>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <asp:GridView ID="GridView1" runat="server" ShowFooter="True" OnRowCreated="GridView1_RowCreated"
               OnRowCommand="GridView1_RowCommand">
          </asp:GridView>
      </div>
  </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Script.Serialization;

public partial class AlphabetPaging : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
     
       if (!IsPostBack)
       {
           if (Session["strTemp"] != null)
           {

               GridView1.DataSource = Session["strTemp"] as DataTable;

               GridView1.DataBind();

           }
           else
           {
               GridView1.DataSource = GetCustomMadeDataTable();
               GridView1.DataBind();
           }
       }

   }
   public DataTable GetCustomMadeDataTable()
   {

       //Create a new DataTable object
       System.Data.DataTable objDataTable = new System.Data.DataTable();
       //Create three columns with string as their type
       objDataTable.Columns.Add("Id", typeof(string));
       objDataTable.Columns.Add("Column1", typeof(string));
       objDataTable.Columns.Add("Column2", typeof(string));
       objDataTable.Columns.Add("Column3", typeof(string));

       //Adding some data in the rows of this DataTable
       DataRow dr;
       for (int i = 65; i <= (65 + 25); i++)
       {

           dr = objDataTable.NewRow();
           dr[0] = i.ToString();
           dr[1] = Char.ConvertFromUtf32(i)+"Column1Data" + i.ToString();
           dr[2] = Char.ConvertFromUtf32(i+1)+"Column2Data" + i.ToString();
           dr[3] = Char.ConvertFromUtf32(i+2)+"Column3Data" + i.ToString();
           objDataTable.Rows.Add(dr);


       }
       DataColumn[] dcPk = new DataColumn[1];
       dcPk[0] = objDataTable.Columns["Id"];
       objDataTable.PrimaryKey = dcPk;
       Session["strTemp"] = objDataTable;


       return objDataTable;
   }
   protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.Footer)
       {

           TableCell cell = e.Row.Cells[0];
           cell.ColumnSpan = 2;

           for (int i = 65; i <= (65 + 25); i++)
           {
               LinkButton lb = new LinkButton();

               lb.Text = Char.ConvertFromUtf32(i) + " ";
              
               lb.CommandArgument = "%" + Char.ConvertFromUtf32(i) + "%";
               lb.CommandName = "AlphaPaging";

               cell.Controls.Add(lb);

           }
       }
   }
   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       //At first I check that if the CommandName is “AlphaPaging”
       if (e.CommandName.Equals("AlphaPaging"))
       {
       
           GridView1.DataSource = GetById(e.CommandArgument.ToString());
           GridView1.DataBind();
       }
   }

   public DataView GetById(string id)
   {

       //Fetch record from database using like operator.
       DataTable dt = new DataTable();
       dt = Session["strTemp"] as DataTable;
       DataView dv = dt.DefaultView;

       dv.RowFilter = "Column1 LIKE '" + id + "'";
       return dv;


   }
}

Comments

  1. thanks Santhosh...
    but where can i bind my data in it

    ReplyDelete
  2. Have a tried my code also check these two events
    GridView1_RowCreated
    GridView1_RowCommand

    ReplyDelete

Post a Comment

Popular posts from this blog

How to convert web page to pdf

In this post i will show how to convert web page to pdf using iTextSharp.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pdf.aspx.cs" Inherits="Pdf" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title></head><body><formid="form1"runat="server"><asp:PlaceHolderID="PlaceholderPdf"runat="server"></asp:PlaceHolder><div><tableborder="1"><tr><tdcolspan="2"> aspdotnetcodebook </td></tr><tr><td> cell1 </td><td> cell2 </td></tr><tr><tdcolspan="2&qu…

Export Html to Pdf using iTextSharp(GridView)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pdf.aspx.cs" Inherits="Pdf" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Untitled Page</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="GridView1"runat="server">
</asp:GridView>
<asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Pdf"/></div>
</form>
</body>
</html>




using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.H…

How to get cell value of gridview using JQUERY

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title><scriptsrc="Scripts/jquery-1.3.2.js"type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { var list = ""; $("#btnGet").click(function() { $("#<%=GridView1.ClientID %> tr").each(function() { //Skip first(header) rowif (!this.rowIndex) return; var age = $(this).find("td:last").html(); list += age + "</br>"; }); …