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
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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With