Sort DataSet

In DataTableI could sort with

 dataTable.DefaultView.Sort = "SortField DESC";

I get DataSetfrom a database, I was wondering if I can sort by DataSethow I do it in DataTable.

+5
source share
7 answers

you can still access the DataTable from the dataset as follows:

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

Hope this helps.

+15
source

DataTable DataSet :

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

, .

+4

DataSet DataTable .

:

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

For advanced sorting tasks, you can use LINQ as described here . This basically allows you to create a DataView from a LINQ query using the System.Data.DataTableExtensions.AsDataFiew extension method .

Alternatively, if you are fine (or perhaps even prefer) using IEnumerable you can use the System.Data.DataTableExtensions.AsEnumerable extension method . For instance:

var enumerable = dataSet.Tables[0].AsEnumerable()
                 .OrderBy(x => x.Field<string>("ColumnName")
                 .ThenByDescending(x => x.Field<int?>("OtherColumnName")??0);
+1
source

Try using the following code.

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

All Articles