WPF DataGrid Sorting Programmactically

Feb 12, 2009 at 1:46 PM
How do you sort a WPF datagrid column in code?  Meaning, if I have two columns ( FirstName and LastName ) how could I sort the LastName column in code to update datagrid?  Thanks
Feb 13, 2009 at 9:30 AM
Hi,

The DataGrid binds to a view of your data, an instance of ICollectionView. This class can be used to perform sorting:

ICollectionView view = CollectionViewSource.GetDefaultView(myDataGrid.ItemsSource);
view.SortDescriptions.Clear();
view.SortDescriptions.Add(new SortDescription("LastName", ListSortDirection.Descending));
view.Refresh();

Regards,
Colin E.
--
http://www.scottlogic.co.uk/blog/wpf/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples


Feb 13, 2009 at 3:02 PM
That did it.  Thanks a bunch.
Dec 14, 2009 at 12:24 AM

And how do I do it with XAML?