Sorting a data table by multiple columns using C #

I have a datatable with columns named foldername , documentname . The data below:

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

How to alphabetically sort a document name based on FolderName in .Net Framework 2.0 .

The solution we tried is given below, but takes too much time, since it contains more than 1200000 entries.

 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; } 
+7
source share
5 answers
  DataTable dt= new DataTable(); DataView dv = new DataView(dt); dv.Sort = "FolderName, DocumentName ASC"; 

Try it. It will sort first for FolderName, then DocumentName.

If you need to send this component to the screen, you can do the same as with the DataTable.

+38
source

Have you tried DataView.Sort?

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

Here is my solution to this problem:

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

If you bind dgv using datatable, you can use something like:

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

Alternatively check this out:

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

thanks

0
source

you can use

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

All Articles