GrapeCity MultiRow Windows Forms Documentation
Sort(String,SortOrder,IComparer) Method
Example 


The name of the cell by which to sort the contents of the GcMultiRow control.
The sort order.
An implementation of System.Collections.IComparer that performs the custom sorting operation.
Sorts the contents of the GcMultiRow control in ascending or descending order based on the contents of the specified column.
Syntax
Public Overloads Sub Sort( _
   ByVal cellName As String, _
   ByVal sortOrder As SortOrder, _
   ByVal comparer As IComparer _
) 
Dim instance As GcMultiRow
Dim cellName As String
Dim sortOrder As SortOrder
Dim comparer As IComparer
 
instance.Sort(cellName, sortOrder, comparer)
public void Sort( 
   string cellName,
   SortOrder sortOrder,
   IComparer comparer
)

Parameters

cellName
The name of the cell by which to sort the contents of the GcMultiRow control.
sortOrder
The sort order.
comparer
An implementation of System.Collections.IComparer that performs the custom sorting operation.
Exceptions
ExceptionDescription
System.ArgumentNullExceptionThe cellName is a null reference (Nothing in Visual Basic) or String.Empty.
System.ArgumentExceptionThe cellName does not match the name of any Cell in the Row.
System.ComponentModel.InvalidEnumArgumentExceptionThe sortOrder is an invalid value for the System.Windows.Forms.SortOrder enumeration.
System.InvalidOperationException

This method cannot be accessed when the Template is a null reference (Nothing in Visual Basic).

-or-

VirtualMode is true and DataSource is a null reference (Nothing in Visual Basic).

Remarks

This method allows advanced customization of the sorting feature in the GcMultiRow class. In order to implement a highly customized sorting operation, write an event handler for the CellMouseClick event for ColumnHeaderCell and call this method with an instance of a class that implements the System.Collections.IComparer interface as a parameter. In this case, you typically set the SortMode property to SortMode.Programmatic to disable automatic sorting and to leave room for a sorting glyph. When sorting by cells set to programmatic sort mode, you must display the sorting glyph yourself by setting the SortGlyphDirection property.

If the AllowUserToAddRows property value is true, the last row is always the last row. In this case, the sorting operator does not effect the last row (uncommitted new row).

Example
The following code example shows how to sort the GcMultiRow control and customize the sort compare logic. To run this example, create a GcMultiRow control in a form with a template. The template should contain at least one cell in the row. You can call this method in a button click event handler or form load event.
void SortByCodeCustomCompare()
        {
            this.gcMultiRow1.Sort(test, SortOrder.Ascending, new StringNumberCompare());
        }

        class StringNumberCompare : System.Collections.IComparer
        {
            public int Compare(object x, object y)
            {
                if (x is string && y is string)
                {
                    int a;
                    int b;
                    if (int.TryParse(x as string, out a) && int.TryParse(y as string, out b))
                    {
                        return a.CompareTo(b);
                    }
                }
                return 0;
            }
        }
Private Sub SortByCodeCustomCompare()
        Me.gcMultiRow1.Sort(test, SortOrder.Ascending, New StringNumberCompare())
    End Sub

    Private Class StringNumberCompare
        Implements System.Collections.IComparer
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            If TypeOf x Is String AndAlso TypeOf y Is String Then
                Dim a As Integer
                Dim b As Integer
                If Integer.TryParse(TryCast(x, String), a) AndAlso Integer.TryParse(TryCast(y, String), b) Then
                    Return a.CompareTo(b)
                End If
            End If
            Return 0
        End Function
    End Class
Requirements

Target Platforms: 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

GcMultiRow Class
GcMultiRow Members
Overload List

 

 


Copyright © GrapeCity, inc. All rights reserved.