SortDescriptions in Silverlight Datagrid
In Silverlight 3, we can specify add sortdescriptions indatagrid so that these columns are sorted initially when the datagrid loads. This can done in XAML as well as in code. The following code snippets demonstrate this behavior.
Specifying SortDescriptions in XAML
<UserControl x:Class="SortDescriptions3.MainPage"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=System.Windows"
>
<Grid x:Name="LayoutRoot" Background="White">
<data:DataGrid x:Name="dg">
<data:DataGrid.SortDescriptions>
<scm:SortDescription PropertyName="City" Direction="Descending" />
<scm:SortDescription PropertyName="Department" Direction="Ascending" />
</data:DataGrid.SortDescriptions>
</data:DataGrid>
</Grid>
</UserControl>
In the above code, we specified the column “City” to be sorted in Descending order, and the column “Department” to be sorted in Ascending order.
Specifying SortDescriptions through code
Defining SortDescriptions on PagedCollectionView
List<Employee> list = PopulateData(100);
PagedCollectionView cv = new PagedCollectionView(list);
cv.GroupDescriptions.Add(new PropertyGroupDescription("City"));
dg.ItemsSource = cv;
cv.SortDescriptions.Add(new SortDescription("FirstName", ListSortDirection.Ascending));
cv.SortDescriptions.Add(new SortDescription("Department", ListSortDirection.Descending));
We can combine sort Descriptions as well as GroupDescriptions (to specify column grouping) as indicated in the above code.
Defining SortDescriptions on datagrid
List<Employee> list = PopulateData(100);
dg.ItemsSource = list;
dg.SortDescriptions.Add(new SortDescription("City", ListSortDirection.Descending));
dg.SortDescriptions.Add(new SortDescription("Department", ListSortDirection.Ascending));
Hooking up the code pieces together and building the application and running it will show us sorting in datagrid.
Comments
Anonymous
March 22, 2009
PingBack from http://blog.a-foton.ru/index.php/2009/03/22/sortdescriptions-in-silverlight-datagrid/Anonymous
March 24, 2009
Does Silverlight 3 has also SortDescriptions for ListBox or only for DataGrid?Anonymous
March 26, 2009
ListBox does not have SortDesciptions. However, if you want to see sorted data in a listbox, then you set the items source to a collection that has sortdescriptions like pagedcollectionview. following code should give some idea List<City> cities = new List<City>(); cities.Add(new City("miami")); cities.Add(new City("los angeles")); cities.Add(new City("Denver")); cities.Add(new City("Austin")); PagedCollectionView cv = new PagedCollectionView(cities); cv.SortDescriptions.Add(new SortDescription("CityName",ListSortDirection.Ascending)); mylistbox.ItemsSource = cv; mylistbox.DisplayMemberPath = "CityName"; City Class: public class City { public string CityName { get; set; } public City(string city) { CityName = city; } }Anonymous
April 08, 2009
Grouping data in Silverlight DataGridAnonymous
April 08, 2009
I got this question on how do you add grouping to the DataGrid in Silverlight without using the RIA ServicesAnonymous
May 13, 2009
I'm not seeing SortDescription Property on DataGrid though I've SL3 installed.Anonymous
August 14, 2009
I'm not see a MyDataGrid.SortDescription in SL3 toAnonymous
February 04, 2010
Comments (required)http://blogs.msdn.com/blogs/JpegImage.aspxEnter Code Here: RequiredRemember Me?Anonymous
July 08, 2010
It looks like the SortDescription property was removed from the grid - it is not in SL4. Instead you have to use a PagedCollectionView and set SortDescription on that.