Multi Grouping in DataGrid in Silverlight 3 Application


Introduction

In this article we will see how Multi 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.

1.gif

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

2.gif

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.

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

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

public
MainPage()
        {
            InitializeComponent(); 

            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"},

            };

 

            MyDataGrid.ItemsSource = myList;

        }

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

3.gif

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:

public
partial class MainPage : UserControl
    {

        PagedCollectionView collection;

 

        public MainPage()

        {

            InitializeComponent();

 

            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"},

            };

 

            collection = new PagedCollectionView(myList);

            collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));

            collection.GroupDescriptions.Add(new PropertyGroupDescription("Gender"));

            MyDataGrid.ItemsSource = collection;

        }

    }

That's it we are done. You can achieve even further grouping. Now run your application and you will find the Country is the first group and then sub grouped by Gender. That means you can choose your own hierarchy.

4.gif

Enjoy Coding.


Recommended Free Ebook
Similar Articles