We recommend that you use the xamDataGrid control instead of the xamGrid control. The xamGrid is being planned for retirement over the next few years and will not receive any new features. We will continue to provide support and critical bug fixes for the xamGrid during this time. 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.
<!-- 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>
Dim MyColumn As New UnboundColumn()
MyColumn.Key = "Total Units"
MyColumn.ValueConverter = New TotalUnitsConverter()
MyColumn.SortComparer = New MySortComparer()
Me.MyDataGrid.Columns.Add(MyColumn)
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.
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
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);
}
}