Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a data generated to datagridview and I replace some values in my datagridview columns.
At some point, I need to sort column4 and column5, but it turns out
like this:

Col3______Col4______Col3
1_________1__________1
241______1__________121
321______1__________161
401______1__________201
81_______1__________41
161______1__________81
250______10________125
330______10________165
410______10________205
90_______10_________45
10_______10_________5
170______10_________85
251______11_________126
331______11_________166
411______11_________206
91_______11_________46
11_______11_________6
171______11_________86

using this code:
DGVtoDT(Dgv1, dt)
dt.DefaultView.Sort = "Col4 ASC, Col5 ASC"
Dgv2.DataSource = dt

it should be
Col3______Col4______Col3
1_________1__________1
81________1__________41
161_______1__________81
241_______1__________121
321_______1__________161
401_______1__________201
2_________2__________1
82________2__________41
162_______2__________81
242_______2__________121
322_______2__________161
402_______2__________201
3_________3__________2
83________3__________42
163_______3__________82
243_______3__________122
323_______3__________162
403_______3__________202

What I have tried:

I also do some gsearch but I didn't found any answer that I wanted
Posted
Updated 5-Oct-22 23:16pm

Without access to your data we can;'t be certain, buit the most likely reason is that those are string values rather than numeric.

String comparisons are based on comparing each character pair in the two strings starting from the left, the first different pair of characters decides the result for the whole comparison - no further character pairs are examined.

So if you compare the numbers 1 to 100 stored as strings, you get this order:
"1"
"10"
"100"
"11"
...
"2"
"20"
...
Check your data source, and use numeric values instead.
 
Share this answer
 
Comments
razrx 6-Oct-22 20:43pm    
Thanks again, somehow I manage to do something with my data table.

Dim dt As New DataTable
Dim r As DataRow

dt.Columns.Add("Col1", GetType(String))
dt.Columns.Add("Col2", GetType(Integer))
dt.Columns.Add("Col3", GetType(Integer))
dt.Columns.Add("Col4", GetType(Integer))
dt.Columns.Add("Col5", GetType(Integer))

For i = 0 To Dgv1.Rows.Count - 1
r = dt.NewRow
r("Col1") = Dgv1.Item(0, i).Value.ToString
r("Col2") = Dgv1.Item(1, i).Value.ToString
r("Col3") = Dgv1.Item(2, i).Value.ToString
r("Col4") = Dgv1.Item(3, i).Value.ToString
r("Col5") = Dgv1.Item(4, i).Value.ToString
dt.Rows.Add(r)
Next

dt.DefaultView.Sort = "Col4 ASC, Col5 ASC"
Dgv2.DataSource = dt
OriginalGriff 7-Oct-22 0:38am    
You're welcome!
As OriginalGriff states above, this is because you are storing numeric values as strings. You just need to change the code in your question at Number series in datagridview VB.NET[^] to store your key values in numeric form, rather than strings.
 
Share this answer
 
v2
Comments
razrx 6-Oct-22 20:45pm    
Thanks for your answer. It also helps me to do some work with my code and I will always keep this in mind. Finally, I can fix my previous issue.

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