Reputation: 19
I've been trying to figure a way of grouping items in DataGrid in code-behind. My DataGrid is filled in code-behind from a List collection of custom Objects, what i wanted is to split this Objects in Groups. Thanks
Upvotes: 0
Views: 6677
Reputation: 13501
You do need to use a CollectionView, but the CollectionView base type does not support grouping.
To get grouping to work in code you need to use one of the derrived CollectionView types that implements grouping such as:
You use it something like this:
ListCollectionView lcv = new ListCollectionView(myCollection);
lcv.GroupDescriptions.Add(new PropertyGroupDescription("PropertyNameToGroupBy"));
MyDataGrid.ItemsSource = lcv;
Normally when you set a collection directly to the ItemSource, WPF will automatically create a CollectionView for you under the covers.
Bea Stollnitz talks a lot about CollectionViews on her blog if you want more info.
Upvotes: 1
Reputation: 106816
You can bind the DataGrid
to a CollectionView
that is created from your list of objects. The CollectionView
supports grouping. This is not really a code-behind solution, but it is very easy to use.
Upvotes: 0