Sort a dataset using C # in a column with a datatype Datetime

I have a dataset (Dataset ds), and below you can find the demo data field inside the dataset. In my Dataset, their column is named Date (Datatype- DateTime), and I want to sort this column. I cannot sort from SQl because a dataset is a merge from two different datasets. Please help me how I sort the data in a dataset.

Date Volume 07/19/201211:30AM 12 07/18/201201:30PM 13 07/17/201203:30PM 22 
+4
source share
5 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 this as a DataSource or list it in foreach . If you need to materialize it, you can create a new DataTable from it:

 DataTable tblOrdered = orderedRows.CopyToDataTable(); 
+8
source

You can do this with a DataView. If the column storing the date and time is a datetime column

 DataView dv = DataTable.DefaultView; dv.Sort = "DateTime-ColumnName ASC"; 

It will not work with a string column containing date and time as a string.

+1
source

You can create data from your data-set .

You can then 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

+1
source

The DataView DataTable has a sort property, passing it the column name

eg.

 dataSet1.Table[0].DefautlView.Sort = "ColumnName"; 

default sort ascending

For more details see the link http://msdn.microsoft.com/en-us/library/system.data.dataview.sort(v=vs.100).aspx

0
source

I believe my solucion adds more columns to the Datatable, where we can save every part of the DateTime :

 Tabla.Columns.Add("year", typeof(DateTime)); Tabla.Columns.Add("month", typeof(DateTime)); Tabla.Columns.Add("day", typeof(DateTime)); 

then we can sort our datatable with a DataView :

 DataView myDataView = DT.DefaultView; case 1: ASC myDataView.Sort = "year asc, month asc, day asc"; case 2: DESC myDataView.Sort = "year desc, month desc, day desc"; Tabla = myDataViwe.toTable(); 

the result is our data type, sorted for dateTime

0
source

All Articles