Infragistics(R) NetAdvantage(R) Windows Forms
SortComparer Property
See Also  Example E-mail your feedback on this topic.
Infragistics.Win.UltraWinGrid Namespace > UltraGridColumn Class : SortComparer Property

Property used to perform custom sort comparisons when sorting rows by this column. The values passed in the Compare method of the IComparer will be two UltraGridCell objects.

Syntax

Visual Basic (Declaration) 
Public Property SortComparer As IComparer
C# 
public IComparer SortComparer {get; set;}

Example

Following code shows you how to sort rows with a custom comparer. It implements SortComparer interface and assigns an instance of it to the SortComparer property of the column.

Visual BasicCopy Code
Imports Infragistics.Shared
Imports Infragistics.Win
Imports Infragistics.Win.UltraWinGrid

  Private Class MySortComparer
      Implements IComparer

      Public Sub New()
      End Sub

      Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare

          ' Passed in objects are cells. So you have to typecast them to UltraGridCell objects first.
          Dim xCell As UltraGridCell = DirectCast(x, UltraGridCell)
          Dim yCell As UltraGridCell = DirectCast(y, UltraGridCell)

          ' Do your own comparision between the values of xCell and yCell and return a negative 
          ' number if xCell is less than yCell, positive number if xCell is greater than yCell, 
          ' and 0 if xCell and yCell are equal.

          ' Following code does an case-insensitive compare of the values converted to string.
          Dim text1 As String = xCell.Value.ToString()
          Dim text2 As String = yCell.Value.ToString()

          Return String.Compare(text1, text2, True)

      End Function

  End Class


  Private Sub Button23_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button23.Click

      Dim band As UltraGridBand = Me.UltraGrid1.DisplayLayout.Bands(0)

      ' Assign an instance of IComparer to SortComparer property.
      band.Columns(0).SortComparer = New MySortComparer()

      ' Sort the column ascending.
      band.SortedColumns.Add(band.Columns(0), False)

  End Sub
C#Copy Code
using Infragistics.Shared;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using System.Diagnostics;

private class MySortComparer : IComparer 
{
	internal MySortComparer( )
	{
	}

	int IComparer.Compare( object x, object y )
	{

		// Passed in objects are cells. So you have to typecast them to UltraGridCell objects first.
		UltraGridCell xCell = (UltraGridCell)x;
		UltraGridCell yCell = (UltraGridCell)y;

		// Do your own comparision between the values of xCell and yCell and return a negative 
		// number if xCell is less than yCell, positive number if xCell is greater than yCell, 
		// and 0 if xCell and yCell are equal.

		// Following code does an case-insensitive compare of the values converted to string.
		string text1 = xCell.Value.ToString( );
		string text2 = yCell.Value.ToString( );

		return String.Compare( text1, text2, true );

	}
}

private void button23_Click(object sender, System.EventArgs e)
{

	UltraGridBand band = this.ultraGrid1.DisplayLayout.Bands[0];
	
	// Assign an instance of IComparer to SortComparer property.
	band.Columns[0].SortComparer = new MySortComparer( );

	// Sort the column ascending.
	band.SortedColumns.Add( band.Columns[0], false );

}

Remarks

This IComparer object will be used for sorting the rows by this column.

The values passed in the ICompare.Compare will be UltraGridCell objects needing comparison.

Care should be taken to ensure that the Infragistics.Win.UltraWinGrid.UltraGridColumn.GroupByEvaluator is logically consistent with the SortComparer. For example, if the sort comparer does a case insensitive sort the group by evaluator logic should be case-insensitive as well.

See Also