使用C#按多列对数据表进行排序

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

Sorting data table by multiple columns using C#

c#sortingdatatable.net-2.0

提问by user1931665

I have a Datatable with columns named foldername,documentname. Data as below:

我有一个名为foldername, 的列的数据表documentname。数据如下:

FolderName  DocumentName
Folder1     HR[D] Document
Folder1     ___----'
Folder1     Asp_example.pdf
Folder2     SD
Folder3     Heavy_weight
Folder3     Accesorial Services

How to alphabeticallysort DocumentName based on FolderName in .Net Framework 2.0.

如何alphabetically基于 FolderName 对 DocumentName进行排序.Net Framework 2.0

Solution we tried is below but takes too many time as it contains more than 1200000 records.

我们尝试的解决方案如下,但由于它包含超过 1200000 条记录,因此花费了太多时间。

int counter=0;

while (counter < searchDT.Rows.Count){
   string FolderName = Convert.ToString(searchDT.Rows[counter]["Folder Name"]);

   string exp = "[Folder Name] like '" + FolderName + "'";

   if (FolderName.Contains("%") || FolderName.Contains("_") || FolderName.Contains("[]") ||      FolderName.Contains("'"))

      exp = "[Folder Name] like '" + EscapeLikeValue(FolderName) + "'";

   string sortExpression = "[Document Name] ASC";

   DataRow[] drfoldername = searchDT.Select(exp, sortExpression);

   foreach (DataRow row in drfoldername)
     drfoldernameDT.ImportRow(row);

   counter += drfoldername.Length;

 }

回答by ?brahim ULUDA?

Have you tried DataView.Sort?

你试过 DataView.Sort 吗?

dt.DefaultView.RowFilter = "FolderName , DocumentName ASC";
dt = dt.DefaultView.ToTable();

回答by André Silva

DataTable dt = new DataTable();

DataView dv = new DataView(dt);
dv.Sort = "FolderName, DocumentName ASC";

Try that out. It will sort first for FolderName, then DocumentName.

试试看。它将首先对 进行排序FolderName,然后是DocumentName

If you need to send that to a component on the screen, you can do the same as you're doing with a DataTable.

如果您需要将其发送到屏幕上的组件,您可以像使用DataTable.

回答by Syed Fahad Ali

If you are binding the dgv using a datatable, you can use do something like :

如果您使用数据表绑定 dgv,则可以执行以下操作:

DataTable dtable = (DataTable) dgv.DataSource; dtable.DefaultView.Sort =

DataTable dtable = (DataTable) dgv.DataSource; dtable.DefaultView.Sort =

Alternatively check this :

或者检查这个:

http://www.codeproject.com/csharp/datagridsort.asp

http://www.codeproject.com/csharp/datagridsort.asp

thanks

谢谢

回答by Scooter

Here was my solution to this problem:

这是我对这个问题的解决方案:

Datatable FI = new Datatable();
DataView viewFI = new DataView(FI);
viewFI.Sort = "ServiceDate, ServiceRoute";
DataTable OFI= viewFI.ToTable();

回答by Amit Kumar Verma

You can use

您可以使用

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