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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"


    xmlns:x="http://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));


 


DataGridSortDescriptions 


Hooking up the code pieces together and building the application and running it will show us sorting in datagrid. 


 


Comments (9)

  1. Roni says:

    Does Silverlight 3 has also SortDescriptions for ListBox or only for DataGrid?

  2. Satish says:

    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;

           }

       }

  3. I got this question on how do you add grouping to the DataGrid in Silverlight without using the RIA Services

  4. Ravi says:

    I’m not seeing SortDescription Property on DataGrid though I’ve SL3 installed.

  5. swo says:

    I’m not see a MyDataGrid.SortDescription in SL3 to

  6. Rodney says:

    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.

Skip to main content