DataGridView - Sort Multiple Columns

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
 
D

Dan Cooperstock

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

That sounds easy, so maybe I'm missing something. Just reverse the order
of the two arguments to the 2nd Compare call, so you are comparing
DataGridViewRow2... to DataGridRow1... That should do it.

--
---------------------------
Dan Cooperstock
DONATION web site: http://www.FreeDonationSoftware.org
DONATION Support forums: http://forums.FreeDonationSoftware.org
E-mail: mailto:[email protected]
Home Phone: 416-423-9064
 
R

Robert Robinson

Hi Dan,

That is a great suggestion. I tried it and it works perfectly.
My only problem is having to hang my head in shame for not having
considered the simple exchange of the rows being compared.
Thank you very much.

Robbie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top