Articles in this section
Category / Section

How to sort your binded collection of ViewModel in WPF DataGrid?

1 min read

By default, DataGrid will not sort the underlying collection when displayed in the grid. When you sort a column, internally datagrid will maintain the underlying collection and does a sort that reflects only in view. So, if you want to sort the underlying collection of view model, you can apply sorting by using SortColumnChanged event.

C#

this.datagrid.SortColumnsChanged += datagrid_SortColumnsChanged;
private void datagrid_SortColumnsChanged(object sender, GridSortColumnsChangedEventArgs e)
{
    var viewModel = this.DataContext as ViewModel;
 
    // view model collection taken
    IEnumerable<BusinessObjects> OrderedSource = viewModel.GDCSource;
 
    foreach (var sortColumn in datagrid.View.SortDescriptions)
    {
        // get property name of current sort column
        var columnName = sortColumn.PropertyName;
 
        if (sortColumn.Direction == ListSortDirection.Ascending)
        // make a sorting by ascending order
            OrderedSource = OrderedSource.OrderBy(b => OrderSource(b, columnName));
        else
        // make a sorting by descending order
            OrderedSource = OrderedSource.OrderByDescending(b => OrderSource(b, columnName));
    }
}
 
private object OrderSource(BusinessObjects b, string name)
{
    var propInfo = b.GetType().GetRuntimeProperty(name);
    if (propInfo != null)
        // get the current sort column value
        return propInfo.GetValue(b);
    return null;
}    

 

Note: In Silverlight, the following code sample can be used to get the current sort column value.

C#

private object OrderSource(OrderInfo b, string name)
{
    var propInfo = b.GetType().GetProperty(name);
    if (propInfo != null)
        // get the current sort column value
        return propInfo.GetValue(b, null);
    return null;
}

 

Here, the underlying collection of SfDataGrid is taken by GetValue. If sortColumn.Direction is ListSortDirection.Ascending the sorting is applied for that collection in ascending direction, that is based on SortDescriptions column, otherwise it will be sorted in descending direction. OrderSource method is used to get the column value.

Sample Links:

WPF

WRT

SilverLight

UWP

 

Conclusion

I hope you enjoyed learning about how to sort your binded collection of ViewModel in WPF DataGrid.

You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Grid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied