Grouping In DataGrid In Silverlight 3 Application

No.of Views521
Bookmarked0 times
Downloads 
Votes0
By  dpatra   On  16 Feb 2010 00:02:01
Tag : Silver Light and XAML , How to
Grouping In DataGrid In Silverlight 3 Application
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 Grouping can be achieved in DataGrid in a Silverlight 3 application.

Creating Silverlight Project
Fire up Visual Studio 2008 and create a Silverlight Application. Name it as GroupInDataGridInSL3.

Image loading...



This is how our application will look like if you design in Expression Blend 3:

grpingdviewsl3img02

I have just added a DataGrid to the application.
For sample data add a class and define your properties in it. I have added a class and named it as Users.cs; I have defined the following properties.


{codecitation class="brush: csharp; gutter: true;" width="650px"}

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



{/codecitation}

Now in MainPage.xaml.cs inside of constructor add some sample data and add it to the Item Source of the DataGrid.

{codecitation class="brush: csharp; gutter: true;" width="650px"}

public MainPage()
{
InitializeComponent();

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

MyDataGrid.ItemsSource = myList;
}


{/codecitation}

Now to test your application run it, and you will find the following screen:

Image Loading...

Suppose we want to group the Users Country wise, in this case users from different countries will be grouped.
To achieve this, you need to use the assembly reference System.Windows.Data.
There is a class called PagedCollectionView, use it as follows:

{codecitation class="brush: csharp; gutter: true;" width="650px"}
public partial class MainPage : UserControl
{
PagedCollectionView collection;

public MainPage()
{
InitializeComponent();

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

collection = new PagedCollectionView(myList);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
MyDataGrid.ItemsSource = collection;
}
}


{/codecitation}

That’s it we are done. Run your application to see the Data in DataGrid grouped by Country:

Image Loading....

Enjoy Coding.

Thank you


About the Author


Diptimaya Patra

Description :I am a Master in Computer Application (MCA) from SRM University, Chennai. I am MCTS in ASP.Net Web Development, and MOSS 2007 Administration. I have extreme exposure to Microsoft Technologies in recent times like Silverlight 2, Silverlight 3. I am from Cuttack, Orissa. You can reach me using this mail (diptimaya.patra@gmail.com). Currently I am working as a Software Engineer in UST Global Inc in Trivandrum Center.

Occupation :Software Engineer
Company : UST Global.
Location : India
Follow me at twitter : http://twitter.com/dpatra


 
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