I am implementing grouping in WPF datagrid. I want to sort the grouped items. For example datagrid is having four columns(empno,name,dept,address). I am doing grouping by dept column. when I click on the dept column header I want to sort the grouped items.
Here I am using ListCollectionView to group the items in the code behind.
public ListCollectionView collection;
collection = new ListCollectionView(obj.empData);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
dgData.Items.SortDescriptions.Add
(new System.ComponentModel.SortDescription
("Country"
,System.ComponentModel.ListSortDirection.Descending
)
);
dgData.Items.SortDescriptions.Add
(new System.ComponentModel.SortDescription
("Contact"
, System.ComponentModel.ListSortDirection.Descending
)
);
dgData.ItemsSource = collection;
private void dgData_Sorting
(object sender, Microsoft.Windows.Controls.DataGridSortingEventArgs e)
{
if (e.Column.SortDirection.ToString() == "Ascending")
{
//dgData.Items.SortDescriptions.Clear();
collection.Refresh();
collection = new ListCollectionView(obj.empData);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
dgData.Items.SortDescriptions.Add
( new System.ComponentModel.SortDescription
("Country"
, System.ComponentModel.ListSortDirection.Descending
)
);
dgData.ItemsSource = collection;
}
}
After changing the sort order it is not reflecting in the UI. Please let me know the correct way to implement this.