Tuesday, June 5, 2007

Adding sort direction arrow into GridView

Call this function @ RowCreated event:
public static void AddSortingArrow(GridView gv, GridViewRowEventArgs e)
{
  if (e.Row.RowType != DataControlRowType.Header)
    return;
  foreach (TableCell tc in e.Row.Cells)
  {
    if (tc.HasControls())
    {
      LinkButton lnk = (LinkButton)tc.Controls[0];
      if (lnk != null)
      {
        Label lbl = new Label();
        lbl.Text = (gv.SortDirection == SortDirection.Ascending ? "▲" : "▼");
        if (gv.SortExpression == lnk.CommandArgument)
        {
            lbl.Text = " " + lbl.Text;
            tc.Controls.Add(lbl);
        }
      }
    }
  }
}

No comments: