对数据集(数据表)进行排序 [英] Sort a dataset (datatable)

查看:102
本文介绍了对数据集(数据表)进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从CSV文件中读取数据并从此输入创建数据集。

添加的每条记录都有一个从1开始的增量ID。

我需要在IDDescending上对数据集进行排序(即反转记录被读入的顺序)但是我没有惨淡失败。



这里包含的基本摘要是什么我试过了。

当我单步浏览并查看DataSet时,它没有排序。



任何人都可以告诉我缺少的东西。



我的尝试:



I am reading data from a CSV file and creating a DataSet from this input.
Each record added has an incremental "ID" starting from 1.
I need to sort the DataSet on "ID" Descending (i.e. reverse the order that the records were read in) but am failing dismally.

Included here is a basic summary of what I've tried.
When I step through and view the DataSet it has not Sorted.

Can anyone please advise what I am missing.

What I have tried:

Dim ds As New DataSet
Dim dt As DataTable = New DataTable()
Dim dr As DataRow

Dim IdCol As DataColumn = New DataColumn("ID", Type.GetType("System.Int32"))
Dim DtCol As DataColumn = New DataColumn("Date", Type.GetType("System.String"))
Dim AmCol As DataColumn = New DataColumn("Amount", Type.GetType("System.String"))
Dim BlCol As DataColumn = New DataColumn("Balance", Type.GetType("System.String"))
Dim DeCol As DataColumn = New DataColumn("Description", Type.GetType("System.String"))

dt.Columns.Add(IdCol)
dt.Columns.Add(DtCol)
dt.Columns.Add(AmCol)
dt.Columns.Add(BlCol)
dt.Columns.Add(DeCol)

dr = dt.NewRow()
dr("ID") = 1
dr("Date") = "22/04/2018"
dr("Amount") = "999.99"
dr("Balance") = "0.00"
dr("Description") = "B) Here is the transaction"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr("ID") = 2
dr("Date") = "23/04/2018"
dr("Amount") = "888.89"
dr("Balance") = "1.00"
dr("Description") = "A) Here is the other transaction"
dt.Rows.Add(dr)

dt.DefaultView.Sort = "ID DESC"

ds.Tables.Add(dt)

ds.Tables("Table1").DefaultView.Sort = "ID DESC"

推荐答案

Sorting和过滤数据| Microsoft Docs [ ^ ]。


这篇关于对数据集(数据表)进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆