Monday, April 03, 2006

Wanna add a sort arrow to your GridView in ASP 2.0? This is an arrow that points upward and downward in the sort column, depending on the direction of the sort.

It's easy, just use the DataBound event like this...


protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRow headerRow = GridView1.HeaderRow;
string strSortExpression = GridView1.SortExpression;
for (int i = 0; i < GridView1.Columns.Count; i++)
{
string strColumnExpression = GridView1.Columns[i].SortExpression;
if ((strSortExpression == strColumnExpression)&&(strSortExpression != ""))
{
Image img = new Image();
if (GridView1.SortDirection.ToString() == "Ascending")
{
img.ImageUrl = "App_Themes/ArrowUp.gif";
}
else
{
img.ImageUrl = "App_Themes/ArrowDown.gif";
}
headerRow.Cells[i].Controls.Add(new LiteralControl(" "));
headerRow.Cells[i].Controls.Add(img);
}
}
}