Version

Please note that this control has been retired and is now obsolete to the XamDataGrid control, and as such, we recommend migrating to that control. It will not be receiving any new features, bug fixes, or support going forward. For help or questions on migrating your codebase to the XamDataGrid, please contact support.

Sorting Unbound Columns

You can sort unbound columns as long as you have an IValueConverter hooked up. However, sorting an unbound column is not as fast and efficient as sorting a normal column, because every cell needs to be evaluated before it is sorted. To achieve the normal level of performance, you should hook up an IComparer to the Columns.SortComparer property. This is the same as creating a custom sort on an existing column. For more information, see the Custom Sort topic.

The following code demonstrates how to set the SortComparer on an unbound column.

In XAML:

<!-- Add a namespace declaration for the ValueConverter to the opening UserControl tag -->
<!-- Add a resource for the converter and sort comparer -->
<UserControl.Resources>
   <helper:TotalUnitsConverter x:Key="TotalUnitsConverter"/>
   <helper:MySortComparer x:Key="MySortComparer"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
   <ig:XamGrid Grid.Row="0" x:Name="MyDataGrid" AutoGenerateColumns="False">
      <ig:XamGrid.Columns>
         <ig:TextColumn Key="ProductID"/>
         <ig:TextColumn Key="ProductName"/>
         <ig:TextColumn Key="UnitPrice"/>
         <ig:TextColumn Key="UnitsInStock"/>
         <ig:TextColumn Key="UnitsOnOrder"/>
         <!-- Create an unbound column, hooking it up to the ValueConverter and a SortComparer -->
         <ig:UnboundColumn Key="Total Units" ValueConverter="{StaticResource TotalUnitsConverter}" SortComparer="{StaticResource MySortComparer}" IsSortable="True"/>
      </ig:XamGrid.Columns>
   </ig:XamGrid>
</Grid>

In Visual Basic:

Dim MyColumn As New UnboundColumn()
MyColumn.Key = "Total Units"
MyColumn.ValueConverter = New TotalUnitsConverter()
MyColumn.SortComparer = New MySortComparer()
Me.MyDataGrid.Columns.Add(MyColumn)

In C#:

this.MyDataGrid.Columns.Add(new UnboundColumn() { Key = "Total Units", ValueConverter = new TotalUnitsConverter(), SortComparer = new MySortComparer() });

The following code is the IComparer that is used in the above example.

In Visual Basic:

Public Class MySortComparer
Inherits IComparer
   Public Function Compare(ByVal x As Product, ByVal y As Product) As Integer
      Dim x1 As Integer = (x.UnitsInStock + x.UnitsOnOrder)
      Dim y1 As Integer = (y.UnitsInStock + y.UnitsOnOrder)
      Return x1.CompareTo(y1)
   End Function
End Class

In C#:

public class MySortComparer : IComparer<Product>
{
   public int Compare(Product x, Product y)
   {
      int x1 = x.UnitsInStock + x.UnitsOnOrder;
      int y1 = y.UnitsInStock + y.UnitsOnOrder;
      return x1.CompareTo(y1);
   }
 }

Related Topics