GrapeCity MultiRow Windows Forms Documentation
Sorting Data

You can sort rows in the GcMultiRow control using single or multiple cell values.

When the GcMultiRow.AllowUserToAddRows property is set to True, the bottom-most row is excluded from sorting.

Sorting Rows Based on Single Cell Value

Use the GcMultiRow.Sort method to sort the rows based on a single cell value. You can specify the cells that will be the basis for sorting, whether the sorting will be in ascending or descending order, or if the sorting needs to differentiate between capital and small letters.

Using Code

The following code uses a default template and sorts the rows based on the value of the second cell.

[VB]

Imports GrapeCity.Win.MultiRow

' Set the default template
GcMultiRow1.Template = Template.Default
GcMultiRow1.AllowUserToAddRows = False
GcMultiRow1.RowCount = 5

' Set the sample data
GcMultiRow1.Rows(0).SetValues(New String(2) {"E", "1", "VB"})
GcMultiRow1.Rows(1).SetValues(New String(2) {"D", "2", "VB"})
GcMultiRow1.Rows(2).SetValues(New String(2) {"C", "3", "CS"})
GcMultiRow1.Rows(3).SetValues(New String(2) {"B", "4", "VB"})
GcMultiRow1.Rows(4).SetValues(New String(2) {"A", "5", "CS"})

' Sort in ascending order with the first cell as the base
GcMultiRow1.Sort(0, SortOrder.Ascending)

[CS]

using GrapeCity.Win.MultiRow;

// Set the default template
gcMultiRow1.Template = Template.Default;
gcMultiRow1.AllowUserToAddRows = false;
gcMultiRow1.RowCount = 5;

// Set the sample data
gcMultiRow1.Rows[0].SetValues(new string[3] {"E", "1", "VB"});
gcMultiRow1.Rows[1].SetValues(new string[3] {"D", "2", "VB"});
gcMultiRow1.Rows[2].SetValues(new string[3] {"C", "3", "CS"});
gcMultiRow1.Rows[3].SetValues(new string[3] {"B", "4", "VB"});
gcMultiRow1.Rows[4].SetValues(new string[3] {"A", "5", "CS"});

// Sort in ascending order with the first cell as the base
gcMultiRow1.Sort(0, SortOrder.Ascending);

Use the GcMultiRow.SortOrder property to get the current sort status if you used the Sort method to sort.

Sort the Rows Based on Multiple Cell Values

If the same value exists in the first cell, and you would like to use the next cell value for sorting, you can set the condition for sorting in the instance of the SortItem class.

Using Code

This example uses the SortItem class.

[VB]

Imports GrapeCity.Win.MultiRow

' Set the default template
GcMultiRow1.Template = Template.Default
GcMultiRow1.AllowUserToAddRows = False
GcMultiRow1.RowCount = 5

' Set the sample data
GcMultiRow1.Rows(0).SetValues(New String(2) {"B", "1", "VB"})
GcMultiRow1.Rows(1).SetValues(New String(2) {"B", "2", "VB"})
GcMultiRow1.Rows(2).SetValues(New String(2) {"C", "3", "CS"})
GcMultiRow1.Rows(3).SetValues(New String(2) {"B", "4", "VB"})
GcMultiRow1.Rows(4).SetValues(New String(2) {"A", "5", "CS"})

' Create the basis for sorting
Dim sortItem1 As New SortItem(0, SortOrder.Ascending)
Dim sortItem2 As New SortItem(1, SortOrder.Ascending)
' Sort the rows
GcMultiRow1.Sort(New SortItem(1) {sortItem1, sortItem2}, 0, GcMultiRow1.RowCount - 1)

[CS]

using GrapeCity.Win.MultiRow;

// Set the default template
gcMultiRow1.Template = Template.Default;
gcMultiRow1.AllowUserToAddRows = false;
gcMultiRow1.RowCount = 5;

// Set the sample data
gcMultiRow1.Rows[0].SetValues(new string[3] {"B", "1", "VB"});
gcMultiRow1.Rows[1].SetValues(new string[3] {"B", "2", "VB"});
gcMultiRow1.Rows[2].SetValues(new string[3] {"C", "3", "CS"});
gcMultiRow1.Rows[3].SetValues(new string[3] {"B", "4", "VB"});
gcMultiRow1.Rows[4].SetValues(new string[3] {"A", "5", "CS"});

// Create the basis for sorting
SortItem sortItem1 = new SortItem(0, SortOrder.Ascending);
SortItem sortItem2 = new SortItem(1, SortOrder.Ascending);
// Sort the rows
gcMultiRow1.Sort(new SortItem[2] {sortItem1, sortItem2}, 0, gcMultiRow1.RowCount - 1);

User-defined Sorting

To independently create a basis for sorting, create a class that implements the System.Collections.IComparer interface and assign its instance to the GcMultiRow.Sort method.

Using Code

The following code gives a higher priority to Null compared to a value.

[VB]

Imports GrapeCity.Win.MultiRow

' Set the default template
GcMultiRow1.Template = Template.Default
GcMultiRow1.AllowUserToAddRows = False
GcMultiRow1.RowCount = 5

' Set the sample data  
GcMultiRow1.Rows(0).Cells(0).Value = 3
GcMultiRow1.Rows(1).Cells(0).Value = 1
GcMultiRow1.Rows(2).Cells(0).Value = 2
GcMultiRow1.Rows(3).Cells(0).Value = Nothing
GcMultiRow1.Rows(4).Cells(0).Value = 0

' Sort the rows  
GcMultiRow1.Sort(0, SortOrder.Ascending, New NullComparer())

' Sort giving priority to Null
Public Class NullComparer
    Implements System.Collections.IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
        Implements System.Collections.IComparer.Compare
        If x = y Then
            Return 0
        End If
        If x Is Nothing Then
            Return -1
        End If
        If y Is Nothing Then
            Return 1
        End If

        Dim ix As Integer = DirectCast(x, Integer)
        Dim iy As Integer = DirectCast(y, Integer)
        Return ix - iy
    End Function
End Class

[CS]

using GrapeCity.Win.MultiRow;

// Set the default template 
gcMultiRow1.Template = Template.Default;
gcMultiRow1.AllowUserToAddRows = false;
gcMultiRow1.RowCount = 5;

// Set the sample data 
gcMultiRow1.Rows[0].Cells[0].Value = 3;
gcMultiRow1.Rows[1].Cells[0].Value = 1;
gcMultiRow1.Rows[2].Cells[0].Value = 4;
gcMultiRow1.Rows[3].Cells[0].Value = null;
gcMultiRow1.Rows[4].Cells[0].Value = 0;

// Sort the rows 
gcMultiRow1.Sort(0, SortOrder.Ascending, new NullComparer());

// Sort giving priority to Null
public class NullComparer : System.Collections.IComparer
{
    int System.Collections.IComparer.Compare(object x, object y)
    {
        if (x == y)
            return 0;
        if (x == null)
            return -1;
        if (y == null)
            return 1;

        int ix = (int)x;
        int iy = (int)y;
        return ix - iy;
    }
}
See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Support Options