SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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();

        DataRow dr = table.NewRow();
            dr["Name"] = item["Title"];
            dr["ID"] = item["ID"];
  GridView.DataSource = table;
share|improve this question
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)?

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")
        else sort_dir="Desc";

        table.DefaultView.sort = sort_col+" "+sort_dir;
        GridView.DataSource= table;

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


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.