Question

When I first load data into a Silverlight DataGrid control, how can I make the screen look exactly as if the user had just clicked the header of the first column? In other words, the data should be sorted in ascending order according to that column's values, AND the little sort arrow should be displayed in the first column's header.

Assuming that's possible, can I also make it look as if the user had clicked the first column and then shift-clicked the second? In other words, can I programatically apply a two-part sort and have the screen look as if the user had done it?

Was it helpful?

Solution

I needed to do this as well, and looked hard and deep at whether the DataGrid could do this.

There IS in fact an appropriate method on the DataGridColumnHeader class, namely InvokeProcessSort, but it's internal and not surfaced anywhere else in the DataGrid classes.

All I've been able to do is pre-sort the data just before I bind it. I'm using a custom data collection that implements ICollectionView as well as ObservableCollection<>, and this works OK - but it's not optimal, and a load of work that's not necessarily needed.

Also, providing a pre-sorted collection means the grid doesn't show the sort marker.

I'll raise this as an issue on the DataGrid bug-tracker - this is a fairly major omission that'd be quite easy to fix - MS just needs to expose appropriate methods on DataGridColumn and DataGrid classes.

OTHER TIPS

This was my solution:

if (pcv.CanSort == true)
{
      pcv.SortDescriptions.Add(new SortDescription("ProductionStatus", ListSortDirection.Ascending));
      pcv.SortDescriptions.Add(new SortDescription("Date", ListSortDirection.Descending));
}

, where pcv is a PageCollectionView. This sorts first by ProductionStatus property and then by Date property. It displays the little sort arrow only for second column though.

Found a good little article by Paul Sherrif that solved this for us...

http://weblogs.asp.net/psheriff/archive/2010/07/14/use-collectionviewsource-in-silverlight.aspx

Basically we were binding the DataGrid to a CollectionViewSource anyway. Turns out this has a CollectionViewSource.SortDescriptions set of config that let us specify the default sort order.

Thanks Paul!

Simply add a SortDescriptor to the DomainDataSource:

<riaControls:DomainDataSource.SortDescriptors>
    <riaControls:SortDescriptor PropertyPath="ColumnName" Direction="Descending" />
</riaControls:DomainDataSource.SortDescriptors>

There exists a CollectionViewSource in Silverlight 3+

http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource(v=vs.95).aspx

There is an easy way to define in XAML the default sort order, as suggested by Microsoft:

Declare sorting in markup using CollectionViewSource

 <Window.Resources>
   <app:MyData x:Key="MyData"/>
   <CollectionViewSource x:Key="cvs" Source={StaticResource MyData}>
     <CollectionViewSource.SortDescriptions>
        <SortDescription Property="Name"/>
     </CollectionViewSource.SortDescriptions>
   </CollectionViewSource>
 </Window.Resources>

 <DataGrid ItemsSource="{Binding Source={StaticResource cvs}}"/> 

found at comments in https://connect.microsoft.com/VisualStudio/feedback/details/678782/set-default-sort-order-of-datagrid

A correction of mine to Microsoft's sample:

 <sort:SortDescription 
   PropertyName="Name"
   Direction="Ascending"
   xmlns:sort="clr-namespace:System.ComponentModel;assembly=System.Windows"
   /> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top