Reputation: 17298
i filled my gridView . Also give property sorting. but i need up down image sorting in progress. Click descending cssclass="sortdescheader". But i can not do that. How can i makie it? İ really used below codes. Please help me with below codes?
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView gridView = (GridView)sender;
if (gridView.SortExpression.Length > 0)
{
int cellIndex = -1;
foreach (DataControlField field in gridView.Columns)
{
if (field.SortExpression == gridView.SortExpression)
{
cellIndex = gridView.Columns.IndexOf(field);
break;
}
}
if (cellIndex > -1)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[cellIndex].CssClass += (gridView.SortDirection == SortDirection.Ascending
? " sortascheader" : " sortdescheader");
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[cellIndex].CssClass += (e.Row.RowIndex % 2 == 0 ? " sortaltrow" : "sortrow");
}
}
}
}
Upvotes: 0
Views: 1881
Reputation: 10865
you can extend the gridview to allow for inserting the sort arrow. this way you can use it in multiple places without duplicating the code: reference: https://web.archive.org/web/20210323170551/http://www.4guysfromrolla.com/articles/012308-1.aspx
public class GridView : System.Web.UI.WebControls.GridView
{
protected override void OnSorted(EventArgs e)
{
string AscCSS = ...;
string DescCSS= ...;
foreach (DataControlField field in this.Columns)
{
// strip off the old ascending/descending icon
field.HeaderStyle.CssClass.Remove();
// See where to add the sort ascending/descending icon
if (field.SortExpression == this.SortExpression)
{
if (this.SortDirection == SortDirection.Ascending)
field.HeaderStyle.CssClass = AscCSS;
else
field.HeaderStyle.CssClass = DescCss;
}
}
base.OnSorted(e);
}
}
Upvotes: 1