DataSet sorting

2019-02-22 05:55发布

In DataTable I could sorting with

 dataTable.DefaultView.Sort = "SortField DESC";

I'm getting a DataSet from database, I was wondering could I do a sorting on the DataSet like how I do it in DataTable.

7条回答
不美不萌又怎样
2楼-- · 2019-02-22 06:27

For advanced sorting needs you might want to use LINQ like described here. Basically it allows creating a DataView from a LINQ query using the System.Data.DataTableExtensions.AsDataFiew extension method.

Alternatively if you are OK with (or maybe even prefer) using an IEnumerable you could use the System.Data.DataTableExtensions.AsEnumerable extension method. For example:

var enumerable = dataSet.Tables[0].AsEnumerable()
                 .OrderBy(x => x.Field<string>("ColumnName")
                 .ThenByDescending(x => x.Field<int?>("OtherColumnName")??0);
查看更多
干净又极端
3楼-- · 2019-02-22 06:32

Access the DataTable from the the DataSet as follows,

ds.Tables[0].DefaultView.Sort = "SortField DESC"; 

Hope this helps.

查看更多
我只想做你的唯一
4楼-- · 2019-02-22 06:40

you can still access the DataTable from the the data set as follows,

ds.Tables[0].DefaultView.Sort =" criterian";

Hope this helps.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-02-22 06:42

Try the following code.

DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
dv.Sort=value;
查看更多
做个烂人
7楼-- · 2019-02-22 06:43

From tha DataSet object, you can access all the DataTable to intract with.

Try this:

DataDet.Tables[0].DefaultView.Sort = "sort criteria";
查看更多
登录 后发表回答