How can I set up my WPF datagrid to sort on multiple columns similar to having two sortable columns, clicking on the header of the first column for a primary sort and then SHIFT clicking on the header of the second column for a secondary sort. I would like the multiple column sort to happen automatically when the user clicks on the header of the first column without having to SHIFT click on the second column header. Is there a way to do this entirely in the xaml? If not how can I do this in the code behind? Currently using VB.Net but a C# snippet is acceptable if you have one. Thanks!
C# – Sort on multiple columns in WPF datagrid
cdatagridvb.netwpf
Best Answer
You can do this by adding System.ComponentModel namespace like this:
then inside the CollectionViewSource XAML add new SortDescriptions like this:
this will sort datagrid on column1,column2.
Edit:
also doing this using C# code behind is pretty easy :
Edit2:
Workaround can be made to catch the column header left mouse click event and prevent the grid from sort on that column like this:
Add this code to the grid PreviewMouseLeftButtonUp event :
You can modify and expand this code to achieve your requirements.