DataSet sorting

↘锁芯ラ 提交于 2020-01-14 07:34:50

问题


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.


回答1:


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

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

Hope this helps.




回答2:


DataView view = ds.Tables[0].DefaultView;

view.Sort = "SortField DESC";

http://msdn.microsoft.com/en-us/library/1ay5y4w0(v=vs.71).aspx

http://social.msdn.microsoft.com/Forums/nl/netfxbcl/thread/adbd95cd-49d1-483d-b2b2-4b696a66e9a6




回答3:


Access the DataTable from the the DataSet as follows,

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

Hope this helps.




回答4:


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

Try this:

DataDet.Tables[0].DefaultView.Sort = "sort criteria";



回答5:


 DataSet fileTransferDetail = null;//Data to be sorted.
 DataSet result = null;//Declare a dataSet to be filled.

//Sort data.
fileTransferDetail.Tables[0].DefaultView.Sort = "ID DESC";
//Store in new Dataset
result.Tables.Add(fileTransferDetail.Tables[0].DefaultView.ToTable());



回答6:


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);



回答7:


Try the following code.

DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
dv.Sort=value;


来源:https://stackoverflow.com/questions/11029823/dataset-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!