Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hello,
how to sort 2 rows in a datagridview with integer values put in textfields. (in visual studio 2010)
The grid is entered programatically without a datasource by the user.
I find lots of info for string sort multiple columns, but i need the field to sort with the value, not the string. on 2 cilumns.
like:

won , score
3, 30
3, 20
3, 10
2, 20
2, 10
1, -5
0, -20
.....
Posted
Updated 9-Dec-11 8:14am
v4
Comments
Sergey Alexandrovich Kryukov 8-Dec-11 16:30pm    
Better don't call it VB. This is VB.NET; change your tags. What's it: WPF, Silverlight, Forms, ASP.NET? Tag it, too.
--SA

1 solution

Please look at my comment to the question.

Right, you don't need string sorting. You either need to sort on the data set and use binding or using custom order via implementation of IComparer. See this discussion: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/c86a387e-3469-489a-a988-c765d70f84d7[^]; see also the articles referenced on this page and MSDN help pages on the mentioned topic.

—SA
 
Share this answer
 
Comments
Loek1961 8-Dec-11 17:21pm    
Hi SA.
Thanks for quick answer. I found this Icomparer routine and i placed it into my programm, but it still sorts on strings. so the problem must be in this part:
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(1).Value.ToString(), _
DataGridViewRow2.Cells(1).Value.ToString())

' If the Last Names are equal, sort based on the First Name.
If CompareResult = 0 Then
CompareResult = System.String.Compare( _
DataGridViewRow1.Cells(0).Value.ToString(), _
DataGridViewRow2.Cells(0).Value.ToString())
End If
Return CompareResult * sortOrderModifier

as you see it still is string compare, but i do have text-values. Should i make up the datagridview with 2 integer columns and then do something about the string compare lines ?
Sergey Alexandrovich Kryukov 8-Dec-11 18:23pm    
It's not about adding implementation of this interface, this is about correct sorting criteria, which you did not even explain. Ordered by fist number first? Second number first? Another rule. Please ask yourself where do you calculate the order of two pairs of numbers.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900