[英]How to sort dataset in VB .net
我正在處理一個代碼,我需要按日期對通過數據庫接收到的數據進行排序(稍后將其導出到 excel 表中,數據應按日期在 excel 表中排序)。
以下是我用來排序的代碼片段。
Dim ds As DataSet
Dim dte As DataTable = ds.Tables(0).DefaultView.ToTable()
dte.DefaultView.Sort = "DATE_START ASC" // Using this to sort
dte.DefaultView.ToTable()
dte.DefaultView.Sort = "DATE_DUE ASC" 返回真實值,但 dte 仍然返回未排序的數據。 不知道這里出了什么問題。 有沒有其他方法可以按日期對數據進行排序?
試試這個 ORDER BY
dte.DefaultView.Sort = "ORDER BY DATE_START ASC"
您已經正確地對數據進行了排序,因此您一定只是錯誤地訪問了它。
'Don't create a DataSet that you don't need.
'If all you need is a DataTable, create a DataTable.
Dim table As New DataTable
'Configure and populate table here.
'Sort the associated view.
table.DefaultView.Sort = "DATE_START"
'This will display the data unsorted, because the DataTable itself is not sorted.
For Each row As DataRow in table.Rows
Console.WriteLine($"{row("ID")}, {row("DATE_START"}")
Next
'This will display the data sorted.
For Each view As DataRowView in table.DefaultView
Console.WriteLine($"{view("ID")}, {view("DATE_START"}")
Next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.