In DataTableI could sort with
DataTable
dataTable.DefaultView.Sort = "SortField DESC";
I get DataSetfrom a database, I was wondering if I can sort by DataSethow I do it in DataTable.
DataSet
you can still access the DataTable from the dataset as follows:
ds.Tables[0].DefaultView.Sort =" criterian";
Hope this helps.
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
DataTable DataSet :
ds.Tables[0].DefaultView.Sort = "SortField DESC";
, .
DataSet DataTable .
:
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 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);
Try using the following code.
DataView dv = new DataView(); dv = ds.Tables[0].DefaultView; dv.Sort=value;