Monday, February 23, 2009

How to implement sorting using .NET Grid view.

ASP.NET Grid view control helps developer to implement technique of sorting a column very easily and effectively.
Grid view has an attribute saying “AllowSorting” which is Boolean and when set to true enables sorting functionality for the grid.
I would like to create a asp.net page that has
1) Text box (Expecting count from user to generate random number from 1 to 100).
2) Button (User when clicks on the button after entering count in the text box, it generates random numbers and bind to grid view).
3) Grid view to display generated random numbers and provide sorting technique.
My aspx page



Code behind page

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;

public partial class Sorting : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void GenerateNumbersAndBindGrid()
{

}
protected void grdSort_Sorting(object sender, GridViewSortEventArgs e)
{
BindGrid(e.SortExpression,(e.SortDirection==SortDirection.Ascending)?"Asc":"Desc");
}
private void BindGrid(string strSortExp,string strSortDire)
{
int intCount = Convert.ToInt32(txtCount.Text);
DataTable dt = Session["DT"] as DataTable;
if (dt == null)
{
Random objRandom = new Random();
DataRow dRow = null;
dt = new DataTable();
dt.Columns.Add(new DataColumn("Num", System.Type.GetType("System.Int32")));
for (int i = 1; i < intCount; i++)
{
dRow = dt.NewRow();
dRow["Num"] = objRandom.Next(100);
dt.Rows.Add(dRow);
}
Session["DT"] = dt;
}
DataView objView = new DataView(dt);
objView.Sort= strSortExp + " " + strSortDire;
grdSort.DataSource = objView;
grdSort.DataBind();
}
private void BindGrid()
{
int intCount = Convert.ToInt32(txtCount.Text);
DataTable dt = Session["DT"] as DataTable;
if (dt == null)
{
Random objRandom = new Random();
DataRow dRow = null;
dt = new DataTable();
dt.Columns.Add(new DataColumn("Num", System.Type.GetType("System.Int32")));
for (int i = 1; i < intCount; i++)
{
dRow = dt.NewRow();
dRow["Num"] = objRandom.Next(100);
dt.Rows.Add(dRow);
}
Session["DT"] = dt;
}
grdSort.DataSource = dt;
grdSort.DataBind();
}
protected void btnGenerate_Click(object sender, EventArgs e)
{
Session["DT"]=null;
BindGrid();
}
}
Aspx page:

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

<!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>Sorting</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table><tr><td>
Count :
</td> <td><asp:TextBox ID="txtCount" runat="server"></asp:TextBox></td></tr>
<tr><td> </td><td><asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="btnGenerate_Click" /></td></tr>
<tr><td colspan="2">
<asp:GridView ID="grdSort" runat="server" AllowSorting="true" OnSorting="grdSort_Sorting">

</asp:GridView>
</td></tr>
</table>
</div>
</form>
</body>
</html>

Output:-









No comments: