如何在c#中按两列对DataTable进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16302901/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:41:46  来源:igfitidea点击:

How to sort DataTable by two columns in c#

c#asp.netsortingdatatable

提问by Mehmet Ince

I have a DataTablethat looks like below;

我有一个DataTable如下所示;

|              ID                    | ItemIndex |   ItemValue
ce895bd9-9a92-44bd-8d79-986f991154a9     1            3
ae7d714e-a457-41a8-8bb4-b5a0471c3d2f     2            2
a774dff3-acc0-4f50-a211-a775e28dcae3     2            1
292bbd50-290b-4511-9e4e-2e74e3ebe273     3            2
ae7d714e-a457-41a8-8bb3-b5a0471c3d22     3            1

I want to sort this table by ItemIndexfirst, then sort the sorted table by ItemValue.

我想先按此表排序ItemIndex,然后按 对已排序表进行排序ItemValue

How can I achieve this?

我怎样才能做到这一点?

Edit: after sorting, I want my table like below;

编辑:排序后,我希望我的表格如下所示;

|              ID                    | ItemIndex |   ItemValue
ce895bd9-9a92-44bd-8d79-986f991154a9     1            3
a774dff3-acc0-4f50-a211-a775e28dcae3     2            1
ae7d714e-a457-41a8-8bb4-b5a0471c3d2f     2            2
ae7d714e-a457-41a8-8bb3-b5a0471c3d22     3            1
292bbd50-290b-4511-9e4e-2e74e3ebe273     3            2

采纳答案by Habib

You can use LINQ to DataSet/DataTable

您可以使用LINQ to DataSet/DataTable

var newDataTable = yourtable.AsEnumerable()
                   .OrderBy(r=> r.Field<int>("ItemIndex"))
                   .ThenBy(r=> r.Field<int>("ItemValue"))  
                   .CopyToDataTable();

回答by George Johnston

Create a DataViewand use the Sort Property:

创建一个DataView并使用 Sort 属性:

DataView dv = new DataView(dt);
dv.Sort = "ItemIndex, ItemValue";

e.g.

例如

foreach (DataRowView row in dv) {
   Console.WriteLine(" {0} \t {1}", row["ItemIndex"], row["ItemValue"]);
}

For more information, check out MDSN for a more thorough example:

有关更多信息,请查看 MDSN 以获取更详尽的示例:

http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx

http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx

回答by Nishant

By creating dataview

通过创建数据视图

var dataView = new DataView(dataTable);
dataView.Sort = "ItemIndex ASC, ItemValue ASC"

Here dataTable is table you want to sort

这里 dataTable 是您要排序的表

回答by Ty Petrice

On the datatable object, just get the defaultview object and set the sort.

在datatable对象上,只需获取defaultview对象并设置排序即可。

dataTable.DefaultView.Sort = "ItemIndex, ItemValue";

回答by Amit Kumar Verma

Alternatively you can use that

或者你可以使用它

DataView oDataSet;
oDataSet.Tables[0].DefaultView.Sort = "Column1 ASC ";

回答by user5940155

_UserAuditTrailTable.DefaultView.Sort = sortExpression;

回答by DarkPh03n1X

Here is my take, given the helpful comments of others here.

鉴于其他人的有用评论,这是我的看法。

DataView dataView = new DataView(dataTable);//datatable to dataview
dataView.Sort = "columnName1 ASC, columnName2 DESC";//string that contains the column name  followed by "ASC" (ascending) or "DESC" (descending)
dataTable = dataView.ToTable();//push the chages back to the datatable