.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak
Subhransu Sekhar Jena
abhays
SP
DotnetUser

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

WPF Grouping In Datagrid

Posted By:Diptimaya Patra       Posted Date: May 09, 2010    Points: 25    Category: WPF    URL: http://dpatra.blogspot.com  

WPF Grouping In Datagrid. In this article we will see how to have Grouped rows in DataGrid in WPF.
 

Introduction

In this article we will see how to have Grouped rows in DataGrid in WPF.

Crating WPF Application Project

Fire up Visual Studio 2008 and Create a WPF Application and name the project as GroupingDataGridWPF.

WPFGrouping1.gif

First we need sample data to load into DataGrid, let's have a Class called Employee which is having the following properties.

public class Employee
{
    public string Name { get; set; }
    public string Contact { get; set; }
    public string EmailID { get; set; }
    public string Country { get; set; }
}

Now in the Constructor we will have some sample data and bind to DataGrid.

public Window1()
{
    InitializeComponent();
    ObservableCollection empData = new ObservableCollection 
    {
        new Employee{Name="Diptimaya Patra", Contact="0000", 
            EmailID="diptimaya.patra@some.com", Country="India"},
        new Employee{Name="Dhananjay Kumar", Contact="00020", 
            EmailID="dhananjay.kumar@some.com", Country="India"},
        new Employee{Name="David Paul", Contact="1230", 
            EmailID="david.paul@some.com", Country="India"},
        new Employee{Name="Christina Joy", Contact="1980", 
            EmailID="christina.joy@some.com", Country="UK"},
        new Employee{Name="Hiro Nakamura", Contact="0000", 
            EmailID="hiro.nakamura@some.com", Country="Japan"},
        new Employee{Name="Angela Patrelli", Contact="0000", 
            EmailID="angela.patrelli@some.com", Country="Japan"},
        new Employee{Name="Zoran White", Contact="0000", 
            EmailID="diptimaya.patra@some.com", Country="Scotland"},
    };

    ListCollectionView collection = new ListCollectionView(empData);
    collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
    dgData.ItemsSource = collection;
}
}


Now that we have Sample data let's see how does it look like.

WPFGrouping4.gif

Looks good to proceed.

Now we will create a Style that will hold the Grouped Data.


  

To use it in DataGrid, use the following XAML inside DataGrid.


  
    
      
        
      
    
  


Now go back to C# code behind and add the Grouping definitions.

ListCollectionView collection = new ListCollectionView(empData);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
dgData.ItemsSource = collection;

Now we are ready to test our application.

WPFGrouping8.gif

Yes, we have achieved grouping. Now we need to have Expand and Collapse functionality of the Group Header.

So in the Style defined for GroupItem let's add an Expander control.


  

Now let's test the application.

WPFGrouping10.gif

Now we can expand and collapse any Grouped Item.

you can also download the sample project used the above example.

Hope this article helps.


 Subscribe to Articles

     

Further Readings:

Responses
Author: Misi         Company URL: http://www.mihaimoisei.ro
Posted Date: March 21, 2012

Hi, great example. :)
How can I display multiple columns or texts in the expander header ?
<Expander.Header>
<TextBlock Text="{Binding Name}"/> <!-- here the group filter will be added (Country)/>-->
<TextBlock Text=" | "/>
<TextBlock Text="{Binding Name2}"/> <!-- here I could have another field />-->
</Expander.Header>

Actually I have 2 tables (Document 1 - * Entry).
I'm displaying the Entries grouped by Documents and I don't want some data to be repeated in the datagrid so I thought to place it in the expander header

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend