0

I have a spgrid with BoundField. I set allowsort to true.

I bind datasource.

but when i click in header dont sort?!

 DataTable table = new DataTable();

        table.Columns.Add("Name");
        table.Columns.Add("ID");
        table.Columns.Add("User");
        table.Columns.Add("UName");
        DataRow dr = table.NewRow();
            dr["Name"] = item["Title"];
            dr["ID"] = item["ID"];
  table.Rows.Add(dr);
  GridView.DataSource = table;
  GridView.DataBind();

2 Answers 2

1

Tried any of these examples (sorting must be "implemented" when using a DataSource other than the a List/View or result of SPQuery)?

  1. http://www.threewill.com/2010/08/the-great-and-powerful-spgridview/
  2. http://sharethefrustration.blogspot.com/2010/02/spgridview-webpart-with-multiple-filter.html
0
1

Dont forget to set the 'onSorting' = 'your_event_handler', and give each sortable column a 'sortExpression'.

I also advise declaring the DataTable as a global variable so you can cache the data for sorting during the event. *Beware the e.SortDirection stays 'Ascending' unless you deal with it, so instead I use a work around and track the current column name & direction to do it myself.

  private static Datatable table;
  private static string sort_col;
  private static string sort_dir;
  private void yourPreviousFunction() { table = new DataTable() etc..}

  public void your_even_handler(object sender, GridViewSortingEventArgs e)
  {
       if(e.sortingExpression.ToString() != sort_col || sort_dir=="Desc")
       {
             sort_col=e.sortingExpression;
             sort_dir="ASC";
        }
        else sort_dir="Desc";

        table.DefaultView.sort = sort_col+" "+sort_dir;
        GridView.DataSource= table;
        GridView.DataBind();
   }

Hope this helps

1
  • And for a multi-column sort.. Datatable.DefaultView.sort ="col1 Dir, col2 Dir"
    – user13186
    Commented Dec 20, 2012 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.