Xceed DataGrid for Silverlight Documentation
Sorting Data

The data that is contained in a grid can be sorted in both ascending and descending order through the use of SortDescriptions objects. Each sort description that is added to the SortDescriptions collection, which is defined on the DataGridControl class, represents one of the possible sort criteria that can be applied to a grid's data. Sort criteria can also be applied by setting a column's SortDirection property, which will result in a sort description being added to the SortDescriptions collection.

<sldg:DataGridControl.SortDescriptions>
 
   <!--The "s" alias references the System.ComponentModel namespace in the the 
          System.Windows assembly
          xmlns:s="clr-namespace:System.ComponentModel;assembly=System.Windows"-->
   <s:SortDescription PropertyName="ShipCountry"/>
   <s:SortDescription PropertyName="ShipCity" Direction="Descending"/>
</sldg:DataGridControl.SortDescriptions>
 
 
 
<sldg:DataGridControl.Columns>
   <sldg:Column FieldName="Freight"                            
                SortDirection="Ascending"/>
</sldg:DataGridControl.Columns>
Whenever groups are created from the values of a column, those same values will automatically be sorted in ascending order; however, a SortDescription object will not be added to the SortDescriptions collection.

End-User Sorting

Through interactions with a ColumnManagerRow, an end user can modify how the data in a grid is sorted. Each column header (see ColumnManagerCell class) can be clicked in order to change its corresponding column's sort direction. Clicking on the column header of an unsorted column will cause its data to be sorted in ascending order. Clicking again will sort the data in descending order. The third click will reestablish the natural sort order (i.e., the corresponding sort description will be removed from the SortDescriptions collection).

If a sort criterion is applied to a column's data and another column header is clicked, the first column's natural sort order will be reestablished and the new column's data will be sorted. To sort multiple columns, the SHIFT key can be held while the column headers are being clicked.


Column-manager row in Signature theme

Collection-Changed Notifications

The SortDescriptions collection is an ObservableCollection, which means that in order to receive notifications as to when its items change (i.e., sort descriptions are added or removed), you can subscribe to its CollectionChanged event. The CollectionChanged event will be raised once for each sort description that is added or removed, either programmatically or through the ColumnManagerRow.

Public Sub New()
   InitializeComponent()
 
   AddHandler sldgDataGridControl.SortDescriptions.CollectionChanged, AddressOf SortDescriptions_CollectionChanged
End Sub
 
Private Sub SortDescriptions_CollectionChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
   Debug.WriteLine("Sort Descriptions Changed")
End Sub
public MainPage()
{
   InitializeComponent();
 
   sldgDataGridControl.SortDescriptions.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(SortDescriptions_CollectionChanged);
}
 
void SortDescriptions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
   Debug.WriteLine("Sort Descriptions Changed");
}

 

Send Feedback