Friday, December 5, 2008

Code for grid view sorting by clicking on the image button which is kept beside the column name in the same column

an empty aspx page

in source................

form id="form1" runat="server">
div
asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="X-Large" Style="z-index: 100;
left: 187px; position: absolute; top: 30px" Text="Sample Sortable GridView"
asp:Label
asp:GridView ID="gvHours" runat="server" AllowSorting="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCreated="gvHours_RowCreated"
OnSorting="gvHours_Sorting" Style="z-index: 102; left: 65px; position: absolute;
top: 84px" Width="513px"
FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"
RowStyle BackColor="#EFF3FB"
Columns
asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"
asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours"
ItemStyle HorizontalAlign="Center"
asp:BoundField
asp:BoundField DataField="Date" DataFormatString="{0:d}" HeaderText="Date" SortExpression="Date"
ItemStyle HorizontalAlign="Center"
asp:BoundField
Columns
PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center"
SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"
HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"
EditRowStyle BackColor="#2461BF"
AlternatingRowStyle BackColor="White"
asp:GridView

div
form


Note: Please keep all the above source code in the their respective formats

in code .cs file/////////////////


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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HoursDAL hoursDAL = new HoursDAL();
ArrayList hourList = hoursDAL.GetHours();
ViewState["HourList"] = hourList;
gvHours.DataSource = hourList;
gvHours.DataBind();
}

private int GetSortColumnIndex()
{
// Iterate through the Columns collection to determine the index
// of the column being sorted.
foreach (DataControlField field in gvHours.Columns)
{
if (field.SortExpression == (string)ViewState["SortExpression"])
{
return gvHours.Columns.IndexOf(field);
}
}
return -1;
}

// This is a helper method used to add a sort direction
// image to the header of the column being sorted.
private void AddSortImage(int columnIndex, GridViewRow headerRow)
{
// Create the sorting image based on the sort direction.
Image sortImage = new Image();
if (GridViewSortDirection == SortDirection.Ascending)
{
sortImage.ImageUrl = "~/images/uparrow.gif";
sortImage.AlternateText = "Ascending Order";
}
else
{
sortImage.ImageUrl = "~/images/downarrow.gif";
sortImage.AlternateText = "Descending Order";
}
// Add the image to the appropriate header cell.
headerRow.Cells[columnIndex].Controls.Add(sortImage);
}


private SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}

private void SortGridView(string sortExpression, string direction)
{
ArrayList hourList = (ArrayList)ViewState["HourList"];
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Hours");
dt.Columns["Hours"].DataType = System.Type.GetType("System.Double");
dt.Columns.Add("Date");
dt.Columns["Date"].DataType = System.Type.GetType("System.DateTime");

foreach (HoursBE hours in hourList)
{
DataRow dr = dt.NewRow();
dr["Name"] = hours.Name;
dr["Hours"] = hours.Hours;
dr["Date"] = hours.Date;
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
gvHours.DataSource = dv;
gvHours.DataBind();
}

private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
protected void gvHours_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
ViewState["SortExpression"] = sortExpression;

if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}

protected void gvHours_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
int sortColumnIndex = GetSortColumnIndex();
if (sortColumnIndex != -1)
{
AddSortImage(sortColumnIndex, e.Row);
}
}
}
}

No comments: