C# 如何对数据集进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/670701/
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
How to Sort Dataset?
提问by Gold
I have Dataset that include table Items, How can I sort this table by Code field ?
我有包含表项的数据集,如何按代码字段对该表进行排序?
thank's in advance
提前致谢
回答by Jesse Weigert
Use a DataView object and call the Sort method on it. Or, if your dataset came from SQL, use the ORDER BY clause to sort it before it gets loaded into the dataset.
使用 DataView 对象并对其调用 Sort 方法。或者,如果您的数据集来自 SQL,请使用 ORDER BY 子句对其进行排序,然后再将其加载到数据集中。
回答by Marc Gravell
With DataTable
, you usually sort a DataView
- for example:
使用DataTable
,您通常对 aDataView
进行排序- 例如:
DataTable table = dataSet.Tables["foo"];
DataView view = table.DefaultView;
view.Sort = "Code";
then work with view
.
然后使用view
.
回答by Taran
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());