Article: Datagrid Sort with LINQ
Share/Save/Bookmark
Description: This topic will show you how to use LINQ to sort a datagrid in an ASP.Net page.

Notes: We will assume that you know how to put a datagrid on your page.  We are also assumming that you have a custom object that you are binding to the datagrid.

First we are going to create two Session Properties to keep the SortExpressions value and the Object Collection from your original search

private string LastSortExpression
{
  
set
   { 
      if( string.IsNullOrEmpty( value ))
      {
         Session.Clear( "LastSortExpression" );
      }
      else
      {
         Session.Add(
"LastSortExpression", value ); 
      }
   }
  
get
   { 
      
return Session["LastSortExpression"].ToString;
   }
}

private MyCustomObject[] MyCustomObjectCollection
{
  
set
   { 
      ifvalue == null )
      {
         Session.Clear( "MyCustomObjectCollection" );
      }
      else
      {
         Session.Add(
"MyCustomObjectCollection", value ); 
      }
   }
  
get
   { 
      
return (MyCustomObject[])Session["MyCustomObjectCollection"];
   }
}

Next, add an enumeration that will mimick the columns in your datagrid.  This will allow you to use reflection to do the sorting.

private enum MyGridsColumns
{
    Column_01 = 1,
    Column_02,
    Column_03,
    Column_04
}

Here are some things to do before we get into the SortCommand on the datagrid
  • You must initially retrieve some data and set the datagrid to the value of your object (E.g. DataSource = myObject[] , DataBind() )
  • Then you must set the Session object to the value of this ( MyCustomObjectCollection = myObject )
  • Then you must define the columns that are going to allow sorting.
  • Make sure that the "SortExpression" property in the BoundColumn is the spelled exactly the same as that of the Enum and the property on the object.
  • Lastly, you must define the Datagrids SortCommand EventHandler ( E.g. SortCommand="myGrid_SortCommand" )

 Lastly, let's look at the SortCommand Event Handler that will perform the sorting

protected void myGrid_SortCommand( object source, DataGridSortCommandEventArgs e )
{
  
if( e != null && !string.IsNullOrEmpty( e.SortExpression ) )
  
{
      // Use Reflection to compare column to enum
     
if( Enum.IsDefined( typeof( MyGridColumns ), e.SortExpression ) )
     
{
        
MyCustomObject[] items = MyCustomObjectCollection;

         if( e.SortExpression == LastSortExpression )
         {
           
items = items.Reverse().ToArray();
            myGrid
.DataSource = items;
        
}
         
else
        
{
           
items = items.OrderBy( i => i.GetType().GetProperty( e.SortExpression ).GetValue( i, null ) ).ToArray();
            myGrid
.DataSource = items;
        
}
         myGrid
.DataBind();
         MyCustomObjectCollection
= items; // Put results into Session
        
LastSortExpression = e.SortExpression; // Put results into Session
        
upResults.Update(); // Update Conditional Update Panel
      
}
  
}
}