Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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();
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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
share|improve this answer
    
Thanks , i use a datatable for bind spgrid. –  ar.gorgin May 9 '12 at 7:26

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

share|improve this answer
    
And for a multi-column sort.. Datatable.DefaultView.sort ="col1 Dir, col2 Dir" –  user13186 Dec 20 '12 at 2:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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