| TechTasks.net |
The Blog is Moved to www.TechTasks.net
GridView paging feature allow us to display fixed number of records on the page and browse to the next page of records. Although paging is a great feature but sometimes we need to view all the items alphabetically. The idea behind this article is to provide a user with a list of all the alphabets and when the user clicks on a certain alphabet then all the records starting with that alphabet will be populated in the GridView control.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
string constr;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
constr = "SELECT * from emp";
binddata(constr);
}
}
public void binddata(string s)
{
string connectionString = "server=TECH-DA98E1DFBA\\SQLEXPRESS;user id=raji;password=chekuri;database=raji";
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(constr , con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
//The RowCreated event is used to create the list.
// In the event first I check for the footer row.
// Once, the footer row is found I run a loop from 65 to 92 and convert each number into the character representation.
// The number 65 stands for “A”, 66 for “B” and so on till 92 for “Z”. Inside the loop I created LinkButton and set the Text property to the alphabet. Finally, the control is added to the cell collection.
//Creating the Alphabetical List:
//The next task is to create an alphabetical list and display it in the GridView control.
//The best place to display the list is the GridView footer. Let’s check out the code which is used to create the list.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TableCell cell = e.Row.Cells[0];
cell.ColumnSpan = 3;
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);
}
}
}
//Fetching the Records Based on the Alphabet:
//In the last section we created the alphabets and displayed them in the footer of the GridView control.
//The next task is to capture the event generated by the alphabets when we click on them and fetch the results based on the alphabet. The RowCommand event is fired whenever you click on any alphabet.
// Take a look at the RowCommand event below:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AlphaPaging"))
{
constr = "select * from emp WHERE ename LIKE '" + e.CommandArgument + "%'";
binddata(constr);
}
}
}
Blog Archive
-
▼
2008
(33)
-
►
December
(14)
- Non Editable Rows in GridView
- Validate DropDownList inside GRIDVIEW
- .Net Keyboard Shortcuts
- Grid View Operataions
- FAQ'S & INTERVIEW TIPS
- C#.NET
- ASP.NET
- DOTNET FRAMEWORK
- Projects With Source Code
- Enter Text In Into Two TextBoxes At the Same Time
- Saving Uploaded file into Disk and displaying the ...
- Writing Connection string in web.config file
- Change GridView Row Color When MouseOver
- Working With Command Builder in ASP.NET
-
▼
November
(14)
- JavaScript Validatoin For Basic Controles in ASP.N...
- Dot Net - C#.Net(FAQ'S)
- Dot Net - Asp.Net - Basic II(FAQ'S)
- Dot Net - Asp.Net - Basic I(FAQ'S)
- Famous Books For .NET
- Connecting MySql With Asp.net Using ODBC Drivers
- Simple Calculator In Windows Application Using C#
- Mail Sending Using Net.Mail
- Select All And Delete Using CheckBox In GridView
- GridView DeleteAll,SelectAll,ClearAll(Server Side)...
- GridView Paging & Sorting
- GridView Update,Delete,Adding (Insert) Record...
- Alphabetical Paging in Gridview
- Connecting MySql With C# ASP.NET
-
►
December
(14)


2 comments:
sir ur answer is perfect but now i want to know the code if the no. of related to any alphabet is greater than page size then again paging will be there.now plz tell me how can we bind that indexing to alphabet.
thanks
[... ] is one useful source of tips on this issue[...]