WPF DataGrid Sorting Programmactically

Feb 12, 2009 at 12: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 8:30 AM

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.Add(new SortDescription("LastName", ListSortDirection.Descending));

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 2:02 PM
That did it.  Thanks a bunch.
Dec 13, 2009 at 11:24 PM

And how do I do it with XAML?