SortDescription In DataGrid In Silverlight 3

No.of Views1142
Bookmarked0 times
Downloads 
Votes0
By  dpatra   On  28 Mar 2010 02:03:07
Tag : Silver Light and XAML , Grid Controls
In this article we will see how can we sort one or more DataGrid Colums using SortDescription method.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article we will see how can we sort one or more DataGrid Colums using SortDescription method.

Creating Silverlight Project

Fire up Visual Studio 2008 and create a Silverlight Application. Name it as SortDescriptionDataGridSL3.
 

Image Loading

Add a DataGrid into your Application.

  

Image Loading

Add a class for defining the sample properties of the Sample Data.

public class Users
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
        public string Country { get; set; }
    }

 Now create some sample data:

List<Users> myList = new List<Users> 
            { 
                new Users{ Name="Hiro Nakamura", Gender="M", Age=24, Country="Japan"},
                new Users{ Name="Mohinder Suresh",Gender="M", Age=26, Country="India"},
                new Users{ Name="Claire Bennette", Gender="F",Age=19, Country="USA"},
                new Users{ Name="Matt Parkman", Gender="M",Age=30, Country="USA"},
                new Users{ Name="Nathan Patrelli", Gender="M",Age=30, Country="USA"},
                new Users{ Name="Peter Patrelli", Gender="M",Age=26, Country="USA"},
                new Users{ Name="Mica", Age=12, Gender="M",Country="USA"},
                new Users{ Name="Linderman", Gender="M",Age=56, Country="USA"},
                new Users{ Name="Ando", Age=24, Gender="M",Country="Japan"},
                new Users{ Name="Maya", Age=24, Gender="M",Country="Mexico"},
                new Users{ Name="Niki Sanders", Gender="F",Age=26, Country="USA"},
                new Users{ Name="Angela Patrelli", Gender="F",Age=26, Country="USA"},
            };

 Now create an instance of PagedCollectionView and give parameter as the above list. 

PagedCollectionView collection = new PagedCollectionView(myList);

Now add SortDescriptions as follows:

collection.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
collection.SortDescriptions.Add(new SortDescription("Country", ListSortDirection.Ascending));

 Now assign the ItemSource of the DataGrid.

MyDataGrid.ItemsSource = collection;


That’s it we have successfully used SortDescription. Now run your application to see the Sort Display:
  

Image Loading

Conclusion

Enjoy the code and please your comments and feed back.

 

 
Sign Up to vote for this article
 
About Author
 
dpatra
Occupation-Not Provided
Company-Not Provided
Member Type-Expert
Location-Not Provided
Joined date-13 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top