vb.net .net - 对数据表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22297201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.net - Sorting a Data Table
提问by Gimo Gilmore
How to sort datatable using VB.NET? I've already read this thread but I having the same output. Sorting a Data TableHow do I sort a datatable
如何使用VB.NET对数据表进行排序?我已经阅读了这个线程,但我有相同的输出。 对数据表进行排序 如何对数据表进行排序
Also, I've tried the sample in Microsoft but still it's not sorting.
另外,我已经在 Microsoft 中尝试过示例,但仍然没有排序。
Dim locationTable As New DataTable("Location")
' Add two columns
locationTable.Columns.Add("State")
locationTable.Columns.Add("ZipCode")
' Add data
locationTable.Rows.Add("Washington", "1")
locationTable.Rows.Add("California", "2")
locationTable.Rows.Add("Hawaii", "3")
locationTable.Rows.Add("Hawaii", "4")
locationTable.Rows.Add("Hawaii", "5")
locationTable.Rows.Add("Hawaii", "6")
locationTable.Rows.Add("Hawaii", "7")
locationTable.Rows.Add("Hawaii", "8")
locationTable.Rows.Add("Hawaii", "9")
locationTable.Rows.Add("Hawaii", "10")
locationTable.AcceptChanges()
Console.WriteLine("Rows in original order" & vbLf & " State " & vbTab & vbTab & " ZipCode")
For Each row As DataRow In locationTable.Rows
Console.WriteLine(" {0} " & vbTab & " {1}", row("State"), row("ZipCode"))
Next
' Create DataView
Dim view As New DataView(locationTable)
' Sort by State and ZipCode column in descending order
view.Sort = "ZipCode ASC"
Console.WriteLine(vbLf & "Rows in sorted order" & vbLf & " State " & vbTab & vbTab & " ZipCode")
For Each row As DataRowView In view
Console.WriteLine(" {0} " & vbTab & " {1}", row("State"), row("ZipCode"))
Next
The output is Rows in sorted order
输出是按排序顺序的行
State ZipCode
州的邮编
Washington 1
华盛顿 1
Hawaii 10
夏威夷 10
California 2
加利福尼亚 2
Hawaii 3
夏威夷 3
Hawaii 4
夏威夷 4
Hawaii 5
夏威夷 5
Hawaii 6
夏威夷 6
Hawaii 7
夏威夷 7
Hawaii 8
夏威夷 8
Hawaii 9
夏威夷 9
It should be 1 ~ 10 right? :(
应该是1~10吧?:(
回答by Vignesh Kumar A
Your Zipcode Column considered as String, so you need to specify Coumn DataType as Int
您的 Zipcode Column 被视为String,因此您需要将 Coumn DataType 指定为Int
Try this
尝试这个
Dim locationTable As New DataTable("Location")
' Add two columns
locationTable.Columns.Add("State",Type.GetType("System.string"))
locationTable.Columns.Add("ZipCode",Type.GetType("System.int32"))
' Add data
locationTable.Rows.Add("Washington", 1)
locationTable.Rows.Add("California", 2)
locationTable.Rows.Add("Hawaii", 3)
locationTable.Rows.Add("Hawaii", 4)
locationTable.Rows.Add("Hawaii", 5)
locationTable.Rows.Add("Hawaii", 6)
locationTable.Rows.Add("Hawaii", 7)
locationTable.Rows.Add("Hawaii", 8)
locationTable.Rows.Add("Hawaii", 9)
locationTable.Rows.Add("Hawaii", 10)
locationTable.AcceptChanges()
Console.WriteLine("Rows in original order" & vbLf & " State " & vbTab & vbTab & " ZipCode")
For Each row As DataRow In locationTable.Rows
Console.WriteLine(" {0} " & vbTab & " {1}", row("State"), row("ZipCode"))
Next
' Create DataView
Dim view As New DataView(locationTable)
' Sort by State and ZipCode column in descending order
view.Sort = "ZipCode ASC"
Console.WriteLine(vbLf & "Rows in sorted order" & vbLf & " State " & vbTab & vbTab & " ZipCode")
For Each row As DataRowView In view
Console.WriteLine(" {0} " & vbTab & " {1}", row("State"), row("ZipCode"))
Next
f you use Dataset...Check like this
如果你使用数据集......像这样检查
If Dataset IsNot Nothing AmdAlso DataSet.Tables(0).Rows.Count>0
// Your Code Here
End IF
回答by Karel Frajták
I think it it works - the ZIP codes are sorted as strings. If you want to sort as numbers, set the type of the Zipcolumn:
我认为它有效 - 邮政编码按字符串排序。如果要按数字排序,请设置Zip列的类型:
locationTable.Columns.Add("ZipCode", typeof(int)) // sorry, I'm a C# guy
回答by Amogh Natu
The reason you are getting that order is because the zip code's data type is mentioned as a string. So between "1" and "10", "1" comes before "10", and between "2" and "10", "2" comes after "10".
您收到该订单的原因是邮政编码的数据类型被提及为字符串。所以在“1”和“10”之间,“1”在“10”之前,在“2”和“10”之间,“2”在“10”之后。
It's like first you get the character '1' so thats placed first. Then the character '1' is found again in the list which is immediately followed by '0' in the row having zipcode 10. So that's placed immediately after the row with zipcode '1'. Then it puts the row with zipcode 2 after 10. Remember they are strings here and not numbers.
这就像首先你得到字符“1”,所以它放在最前面。然后在列表中再次找到字符“1”,该字符紧跟在邮政编码为 10 的行中的“0”之后。因此,它紧跟在邮政编码为“1”的行之后。然后它将邮政编码为 2 的行放在 10 之后。记住它们在这里是字符串而不是数字。
You only need to change the type of zipcode column to integer.
您只需要将邮政编码列的类型更改为整数。
回答by Gimo Gilmore
What I did it to add new column. And get the old value and save it to the new column
我做了什么来添加新列。并获取旧值并将其保存到新列
oDetail.Columns.Add("new_line", GetType(Integer))
Dim i As Integer = 1
For Each row In oDetail.Rows
row("new_line") = i
i += 1
Next
oDetail.DefaultView.Sort = "new_line DESC"

