R
Robert Robinson
I am using the following code to sort two columns; for example,
lastName, firstName.
Public Class ARowComparer
Implements System.Collections.IComparer
Private sortOrderModifier As Integer = 1
Public Sub New(ByVal sortOrder As SortOrder)
If sortOrder = sortOrder.Descending Then
sortOrderModifier = -1
ElseIf sortOrder = sortOrder.Ascending Then
sortOrderModifier = 1
End If
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer _
Implements System.Collections.IComparer.Compare
Dim DataGridViewRow1 As DataGridViewRow = CType(x, DataGridViewRow)
Dim DataGridViewRow2 As DataGridViewRow = CType(y, DataGridViewRow)
' Try to sort based on the Last Name column.
Dim CompareResult As Integer = System.String.Compare( _
DataGridViewRow1.Cells(3).Value.ToString(), _
DataGridViewRow2.Cells(3).Value.ToString())
' If the Last Names are equal, sort based on the First Name.
If CompareResult = 0 Then
CompareResult = System.String.Compare( _
DataGridViewRow1.Cells(2).Value.ToString(), _
DataGridViewRow2.Cells(2).Value.ToString())
End If
Return CompareResult * sortOrderModifier
End Function
End Class
The code works fine, but I need to be able to sort a first column in
ascending order and a second in descending order. The present code sorts
both columns in either ascending or descending order.
Does anyone know the best approach for a multiple column sort that has
this capability.
Thank you very much.
Robbie
lastName, firstName.
Public Class ARowComparer
Implements System.Collections.IComparer
Private sortOrderModifier As Integer = 1
Public Sub New(ByVal sortOrder As SortOrder)
If sortOrder = sortOrder.Descending Then
sortOrderModifier = -1
ElseIf sortOrder = sortOrder.Ascending Then
sortOrderModifier = 1
End If
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer _
Implements System.Collections.IComparer.Compare
Dim DataGridViewRow1 As DataGridViewRow = CType(x, DataGridViewRow)
Dim DataGridViewRow2 As DataGridViewRow = CType(y, DataGridViewRow)
' Try to sort based on the Last Name column.
Dim CompareResult As Integer = System.String.Compare( _
DataGridViewRow1.Cells(3).Value.ToString(), _
DataGridViewRow2.Cells(3).Value.ToString())
' If the Last Names are equal, sort based on the First Name.
If CompareResult = 0 Then
CompareResult = System.String.Compare( _
DataGridViewRow1.Cells(2).Value.ToString(), _
DataGridViewRow2.Cells(2).Value.ToString())
End If
Return CompareResult * sortOrderModifier
End Function
End Class
The code works fine, but I need to be able to sort a first column in
ascending order and a second in descending order. The present code sorts
both columns in either ascending or descending order.
Does anyone know the best approach for a multiple column sort that has
this capability.
Thank you very much.
Robbie