Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set initial sort order in Silverlight DataGrid?

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?

like image 626
Eric Avatar asked Oct 22 '08 15:10

Eric


2 Answers

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.

like image 149
Valentin Avatar answered Nov 16 '22 01:11

Valentin


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.

like image 34
Rammesses Avatar answered Nov 16 '22 01:11

Rammesses