Xceed DataGrid for WPF v7.2 Documentation
SortIndex Property
Example 


Xceed.Wpf.DataGrid Assembly > Xceed.Wpf.DataGrid Namespace > ColumnBase Class : SortIndex Property
Gets the sort index of the column.
Syntax
'Declaration
 
Public ReadOnly Property SortIndex As Integer
'Usage
 
Dim instance As ColumnBase
Dim value As Integer
 
value = instance.SortIndex
public int SortIndex {get;}

Property Value

A value representing the sort index of the column. If -1, the column is not sorted. By default, -1.
Remarks
The sort index represents the order in which the column was sorted. For example, if 2 columns are sorted, the first one that was sorted will have its SortIndex property set to 0 while the second will have its SortIndex property set to 1. Columns which are not sorted will have their SortIndex property set to -1.
Example
All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.
The following example demonstrates how to sort the data items in an ascending direction according to the values of the ShipCountry column.The following example demonstrates how to provide a custom sort comparer that sorts addresses. The AddressComparer class (provided below) will first sort addresses which begin with numeric values by street name and then civic number. Address that do not have a civic number will be sorted alphabetically.The following code provides the implementation of the AddressComparer class.The following code provides the implementation of the AddressComparer class.
<Grid xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
      xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"> 
  <Grid.Resources> 
    <xcdg:DataGridCollectionViewSource x:Key="cvs_orders" 
                                     Source="{Binding Source={x:Static Application.Current}, 
                                                       Path=Orders}"> 
 
     <xcdg:DataGridCollectionViewSource.SortDescriptions> 
       <scm:SortDescription PropertyName="ShipCountry" Direction="Ascending"/> 
     </xcdg:DataGridCollectionViewSource.SortDescriptions> 
    </xcdg:DataGridCollectionViewSource>           
  </Grid.Resources> 
 
  <xcdg:DataGridControl x:Name="OrdersGrid" 
                        ItemsSource="{Binding Source={StaticResource cvs_orders}}"> 
    <xcdg:DataGridControl.Columns> 
      <xcdg:Column FieldName="ShipCountry" VisiblePosition="0"/> 
    </xcdg:DataGridControl.Columns> 
  </xcdg:DataGridControl> 
</Grid>
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid" 
      xmlns:local="clr-namespace:Xceed.Wpf.Documentation"> 
   <Grid.Resources> 
      <local:AddressComparer x:Key="addressComparer"/> 
      <xcdg:DataGridCollectionViewSource x:Key="cvs_orders" 
                                         Source="{Binding Source={x:Static Application.Current}, 
                                                          Path=Orders}" 
                                         AutoCreateItemProperties="False"> 
         <xcdg:DataGridCollectionViewSource.ItemProperties> 
            <xcdg:DataGridItemProperty Name="ShipCountry" /> 
            <xcdg:DataGridItemProperty Name="ShipCity" /> 
            <xcdg:DataGridItemProperty Name="ShipAddress" 
                                       SortComparer="{StaticResource addressComparer}"/> 
            <xcdg:DataGridItemProperty Name="ShipVia" /> 
         </xcdg:DataGridCollectionViewSource.ItemProperties> 
      </xcdg:DataGridCollectionViewSource> 
   </Grid.Resources> 
   <xcdg:DataGridControl x:Name="OrdersGrid" 
                         ItemsSource="{Binding Source={StaticResource cvs_orders}}">          
      <xcdg:DataGridControl.View> 
         <xcdg:TableView ColumnStretchMode="StretchAll"/> 
      </xcdg:DataGridControl.View> 
   </xcdg:DataGridControl> 
</Grid>
Imports System
Imports System.Collections
Imports System.Data
Namespace Xceed.Wpf.Documentation

  Public Class AddressComparer
               Implements IComparer

    Public Sub New()
    End Sub
    Public Function Compare( x As Object, y As Object ) As Integer Implements IComparer.Compare
      Dim stringX As String = CType( x, String )
      Dim stringY As String = Ctyle( y, String )
      Const digits As String = "0123456789"
      If( ( digits.IndexOf( stringX( 0 ) ) >= 0 ) And ( digits.IndexOf( stringY( 0 ) ) >= 0 ) ) Then
        Dim index As Integer = 0
        Dim xNumber As System.Text.StringBuilder = New System.Text.StringBuilder()
        While( ( index < stringX.Length ) And ( digits.IndexOf( stringX( index ) ) >= 0 ) )
          xNumber.Append( stringX( index ) )
          index++
        End While
        index = 0
        Dim yNumber As System.Text.StringBuilder = New System.Text.StringBuilder()
        While( ( index < stringY.Length ) And ( digits.IndexOf( stringY( index ) ) >= 0 ) )
          yNumber.Append( stringY( index ) )
          index++
        End While
        Dim xValue = Long.Parse( xNumber.ToString() )
        Dim yValue As Long = Long.Parse( yNumber.ToString() )
        If( xValue > yValue ) Then
          Return 1
        End If
        If( xValue < yValue ) Then
          Return -1
        End If
        Return stringX.CompareTo( stringY )
      Else
        Return stringX.CompareTo( stringY )
      End If
    End Function
  End Class
End Namespace
using System;
using System.Collections;
using System.Data;
namespace Xceed.Wpf.Documentation
{
public class AddressComparer: IComparer
{
  public AddressComparer()
  {
  }

  int IComparer.Compare( object x, object y )
  {
    string stringX = ( string )x;
    string stringY = ( string )y;

    const string digits = "0123456789";
    if( ( digits.IndexOf( stringX[ 0 ] ) >= 0 ) && ( digits.IndexOf( stringY[ 0 ] ) >= 0 ) )
    {
      int index = 0;
      System.Text.StringBuilder xNumber = new System.Text.StringBuilder();

      while( ( index < stringX.Length ) && ( digits.IndexOf( stringX[ index ] ) >= 0 ) )
      {
        xNumber.Append( stringX[ index ] );
        index++;
      }

      index = 0;
      System.Text.StringBuilder yNumber = new System.Text.StringBuilder();

      while( ( index < stringY.Length ) && ( digits.IndexOf( stringY[ index ] ) >= 0 ) )
      {
        yNumber.Append( stringY[ index ] );
        index++;
      }

      long xValue = long.Parse( xNumber.ToString() );
      long yValue = long.Parse( yNumber.ToString() );

      if( xValue > yValue )
        return 1;

      if( xValue < yValue )
        return -1;

      return stringX.CompareTo( stringY );
    }
    else
    {
      return stringX.CompareTo( stringY );
    }
  }
}
}
Requirements

Target Platforms: Windows 11, Windows, 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

ColumnBase Class
ColumnBase Members