简体   繁体   中英

DataSet sorting

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 .

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

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

Hope this helps.

Access the DataTable from the the DataSet as follows,

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

Hope this helps.

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

Try this:

DataDet.Tables[0].DefaultView.Sort = "sort criteria";
 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());

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

Try the following code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM