Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataset Sorting using C# on a column with Datetime datatype

Tags:

c#

asp.net

I am having a dataset (Dataset ds) and below you can find the field with demo data inside the dataset. In my Dataset , their is column with name Date (Datatype- DateTime) and i want to sort this column.I cant do the sorting from SQl because the Dataset is a merge of 2 Different Dataset. Please help me that how i do the sorting in dataset.

Date              Volume

07/19/201211:30AM  12

07/18/201201:30PM  13

07/17/201203:30PM  22
like image 232
Dharmendra Kumar Singh Avatar asked Dec 13 '22 00:12

Dharmendra Kumar Singh


2 Answers

This is a simple Linq-To-DataSet approach:

IEnumerable<DataRow> orderedRows = dataSet.Tables[0].AsEnumerable()
    .OrderBy(r => r.Field<DateTime>("Date"));

Now you can use that as DataSource or enumerate it in a foreach. If you need to materialize it, you might want to create a new DataTable from it:

DataTable tblOrdered = orderedRows.CopyToDataTable();
like image 94
Tim Schmelter Avatar answered May 23 '23 01:05

Tim Schmelter


You can create a data view from your data-set.

Then you can use the DataView.Sort Property to sort your data.

eg.

   DataView myDataView = DT.DefaultView;  
   myDataView.Sort = "Date DESC";

Read more about Introduction to Filtering and Sorting in Datasets

like image 33
huMpty duMpty Avatar answered May 23 '23 02:05

huMpty duMpty