Can't find what you're looking for?

Contact us or post in our forums and we'll help you find an answer.

Sort Columns Using Absolute Values in C1DataGrid for WPF

Background:

Often times users need the capability to sort columns in the C1DataGrid by absolute values that do not change. Here is how that is done.

Steps to Complete:

1. Create custom logic by implementing IComparer interface

class SortByAbsolute : IComparer
{
    public int Compare(Sales x, Sales y)
    {
        double val1 = (double)x.Growth;
        double val2 = (double)y.Growth;
        var result = System.Collections.Comparer.Default.Compare(Math.Abs(val1), Math.Abs(val2));
        return result;
    }
}

2. Use that custom logic to sort the individual column that needs sorted by absolute value using the SortChanging Event.

List _list;
private void _grid_SortChanging(object sender, C1.WPF.DataGrid.DataGridSortChangingEventArgs e)
{
     if (e.ChangingColumns[0].Column.Name == "Growth")
     {
         e.Cancel = true;
         _grid.ItemsSource = null;
         SortByAbsolute sort = new SortByAbsolute();
         _list.Sort(sort);
         _grid.ItemsSource = _list;
     }
}

Get the Latest Version of WPF Edition
Download the Free Trial Now