在具有日期时间数据类型的列上使用 C# 进行数据集排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11556114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dataset Sorting using C# on a column with Datetime datatype
提问by Dharmendra Kumar Singh
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.
我有一个数据集 (Dataset ds),您可以在下面找到数据集内包含演示数据的字段。在我的数据集中,它们是名称为 Date (Datatype-DateTime) 的列,我想对此列进行排序。我无法从 SQl 进行排序,因为数据集是 2 个不同数据集的合并。请帮助我了解如何在数据集中进行排序。
Date Volume
07/19/201211:30AM 12
07/18/201201:30PM 13
07/17/201203:30PM 22
采纳答案by Tim Schmelter
This is a simple Linq-To-DataSetapproach:
这是一个简单的Linq-To-DataSet方法:
IEnumerable<DataRow> orderedRows = dataSet.Tables[0].AsEnumerable()
.OrderBy(r => r.Field<DateTime>("Date"));
Now you can use that as DataSourceor enumerate it in a foreach. If you need to materialize it, you might want to create a new DataTablefrom it:
现在您可以将其用作DataSource或在foreach. 如果您需要实现它,您可能希望从中创建一个新的DataTable:
DataTable tblOrdered = orderedRows.CopyToDataTable();
回答by Yograj Gupta
You can do this by a DataView. If your column which is holding datetime is a datetime column
您可以通过 DataView 执行此操作。如果您保存日期时间的列是日期时间列
DataView dv = DataTable.DefaultView;
dv.Sort = "DateTime-ColumnName ASC";
It will not work on string column holding datetime as string.
它不适用于将日期时间保存为字符串的字符串列。
回答by HatSoft
The DataView of a DataTable have a sort property please pass the column name to it
DataTable 的 DataView 有一个排序属性,请将列名传递给它
E.g.
例如
dataSet1.Table[0].DefautlView.Sort = "ColumnName";
default sort is by ascending order
默认排序是升序
For more see link http://msdn.microsoft.com/en-us/library/system.data.dataview.sort(v=vs.100).aspx
有关更多信息,请参阅链接http://msdn.microsoft.com/en-us/library/system.data.dataview.sort(v=vs.100).aspx
回答by huMpty duMpty
You can create a dataview from your data-set.
您可以创建一个数据从您的视图data-set。
Then you can use the DataView.Sort Propertyto sort your data.
然后您可以使用DataView.Sort 属性对您的数据进行排序。
eg.
例如。
DataView myDataView = DT.DefaultView;
myDataView.Sort = "Date DESC";
Read more about Introduction to Filtering and Sorting in Datasets
阅读有关数据集中过滤和排序简介的更多信息
回答by Freddy Santacruz
I find my solucion is add more columns to Datatable where we can save each part of a DateTime:
我发现我的解决方案是向 Datatable 添加更多列,我们可以在其中保存 a 的每个部分DateTime:
Tabla.Columns.Add("year", typeof(DateTime));
Tabla.Columns.Add("month", typeof(DateTime));
Tabla.Columns.Add("day", typeof(DateTime));
then we can sortour datatable with a DataView:
然后我们可以使用sort我们的数据表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();
result is our datatable sorted for a dateTime
结果是我们按日期时间排序的数据表
回答by Manali
Using Dataview you can sort your Datatable. Just put your Datatable in Dataview Look at the following example :
使用 Dataview,您可以对数据表进行排序。只需将您的数据表放在 Dataview 中查看以下示例:
You have dtlog table which having two columns one is date and another one is detials. you want to sort your datatable by date column.
您有 dtlog 表,其中有两列,一列是日期,另一列是详细信息。您想按日期列对数据表进行排序。
DataTable dtlog = new DataTable();
dtlog.Columns.Add("date");
dtlog.Columns.Add("details");
1.create dataview and put your datatable in view
1.创建数据视图并将您的数据表放在视图中
DataView dtview = new DataView(dtlog);
string sortstring = "date DESC"; // sorting in descending manner
dtview.Sort = sortstring;
2.Create another table where you can save your sorted dataview
2.创建另一个表,您可以在其中保存已排序的数据视图
DataTable dtsort =dtview.ToTable();

