vb.net 如何对数据表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19505979/
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
How do I sort a datatable
提问by BClaydon
How do I sort a datatable? I need to return a datatable from a function. I have been struggling with this for hours, and the internet has a few different answers, none of which seem to work for me.
如何对数据表进行排序?我需要从函数返回一个数据表。我已经为此苦苦挣扎了几个小时,互联网上有一些不同的答案,但似乎没有一个对我有用。
Edit: I want to punch myself. Do a DataView.Sort on your table, then a DataView.ToTable() to put the sorted data into a new dataset... Example:
编辑:我想打自己。在你的表上做一个 DataView.Sort,然后一个 DataView.ToTable() 把排序的数据放到一个新的数据集中......例如:
Dim view As New DataView(OriginalDataSet) 'Put your original dataset into a dataview
view.Sort = "ColumnName" ' Sort your data view
Dim NewDataSet As DataTable = view.ToTable() ' Put your dataview into a new datatable
End of example
示例结束
I have a relatively simple example table below, taken from a teaching website. The one twist is that there are duplicate values in the row I am trying to sort on.
下面我有一个比较简单的示例表,取自教学网站。一个转折是我试图排序的行中有重复的值。
Module Module1
Sub Main()
' Get a DataTable instance from helper function.
Dim table As DataTable = GetTable()
End Sub
''' <summary>
''' Helper function that creates new DataTable.
''' </summary>
Function GetTable() As DataTable
' Create new DataTable instance.
Dim table As New DataTable
' Create four typed columns in the DataTable.
table.Columns.Add("Dosage", GetType(Integer))
table.Columns.Add("Drug", GetType(String))
table.Columns.Add("Patient", GetType(String))
table.Columns.Add("Date", GetType(DateTime))
' Add five rows with those columns filled in the DataTable.
table.Rows.Add(25, "Indocin", "David", DateTime.Now)
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)
table.Rows.Add(21, "Aspirin", "Janet", DateTime.Now)
Return table
End Function
End Module
I have tried selecting into an array, then looping through the array and putting each row into a new datatable, but the select isn't grabbing rows. Example:
我试过选择一个数组,然后遍历数组并将每一行放入一个新的数据表中,但选择没有抓取行。例子:
drarray = ds.Select("I want to select all here", "MySortColumn")
drarray = ds.Select("我想在这里全选", "MySortColumn")
I have tried various looping strategies, with inner loops, etc and can't seem to figure that out.
我尝试了各种循环策略,使用内循环等,但似乎无法弄清楚。
I have tried dataTable.DefaultView.Sort = "sortExp" but I can't get that to work.
我试过 dataTable.DefaultView.Sort = "sortExp" 但我不能让它工作。
So what am I missing? I figure with the DefaultView and Select methods I'm just missing something syntactly.
那么我错过了什么?我认为 DefaultView 和 Select 方法只是在语法上遗漏了一些东西。
So what's the best way to go, and what am I missing?
那么最好的方法是什么,我错过了什么?
采纳答案by ps2goat
Use a DataView to create a view of your data in the DataTable. This allows you to sort, filter, etc. Here's a C# example: Datatable VS dataview
使用 DataView 在 DataTable 中创建数据视图。这允许您进行排序、过滤等。这是一个 C# 示例:Datatable VS dataview
回答by Steve
You can use something like this:
你可以使用这样的东西:
Return table.Select("","Columns to sort on").CopyToDataTable
回答by ajit
This may help you sortExp can be field on which based the sort should be performed filterExp that should evaluate to true or false. assuming the following fields
这可能会帮助您 sortExp 可以是应基于其执行排序的字段 filterExp 应评估为 true 或 false。假设以下字段
Dim filterExp As String = "Patient<> ''"
Dim sortExp As String = "Date "
dt_item.Select(filterExp, sortExp, DataViewRowState.CurrentRows)
The above code shows how to filter and sort the data table dt_item. The filter expression selects Date whose Patient is not NULL. The sort expression causes the results to be sorted by the Date column
上面的代码展示了如何对数据表dt_item进行过滤和排序。过滤器表达式选择其 Patient 不为 NULL 的 Date。排序表达式使结果按日期列排序