essedbl
essedbl

Reputation: 520

Sort Autofilter contents - XCeed WPF Datagrid

I am using an Xceed WPF Datagrid in an app. I have enabled Autofilter on one of the columns, but the contents are not sorted. I can't figure out if there is a property or something, a style perhaps, to tell the thing to sort alphabetically. Has anyone had any experience with this?

Unfortunately, when I google search, or even search on Xceed's website, everything related to sorting is sorting the rows by clicking a column header. But I want the list of options in the autofilter dropdown to be sorted...

Thanks, Nathaniel D. Holcomb

Upvotes: 0

Views: 1360

Answers (1)

Nerd in Training
Nerd in Training

Reputation: 2230

You can set the DistinctValuesSortComparer property on the ItemProperty that represents your column and do your custom sorting within the comparer.

I believe they have this set in their sample application.

For example:

C#

public class MonthNamesDistinctValuesSortComparer : IComparer
  {
    public MonthNamesDistinctValuesSortComparer()
    {
      for( int i = 0; i < DateTimeFormatInfo.CurrentInfo.MonthNames.Length; i++ )
      {
        string monthName = DateTimeFormatInfo.CurrentInfo.MonthNames[ i ];
        m_monthNameToIndex.Add( monthName, i );
      }
    }

    #region IComparer Members

    public int Compare( object x, object y )
    {
      string xMonth = x as string;
      string yMonth = y as string;

      if( ( xMonth != null ) && ( yMonth != null ) )
      {
        int xIndex = m_monthNameToIndex[ xMonth ];
        int yIndex = m_monthNameToIndex[ yMonth ];

        if( xIndex < yIndex )
        {
          return -1;
        }
        else if( xIndex == yIndex )
        {
          return 0;
        }
        else
        {
          return 1;
        }
      }

      // Unable to compare, return 0 (equals)
      return 0;
    }

    #endregion

    private Dictionary<string, int> m_monthNameToIndex = new Dictionary<string, int>();
  }

XAML

<local:MonthNamesDistinctValuesSortComparer x:Key="monthNamesDistinctValuesSortComparer" />
<xcdg:DataGridItemProperty Name="ShippedDate"
                                          Title="Shipped Date"
                                          DistinctValuesSortComparer="{StaticResource monthNamesDistinctValuesSortComparer}"
                                          QueryDistinctValue="OnShippedDateQueryDistinctValue" />

Upvotes: 1

Related Questions