views:

784

answers:

1

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");
                }
            }
        }
    }
+1  A: 

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: 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);
   }
}
Russ Bradberry
"field.HeaderStyle.CssClass.Remove();" not working Russ....
Phsika
my apologies, i did not have this loaded up in the ide when i made the changes. try just setting it to a blank string OR to your normal CSS class
Russ Bradberry