Sorting data table by multiple columns using C#

50,696

Solution 1

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.

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

Solution 2

Here was my solution to this problem:

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

Solution 3

Have you tried DataView.Sort?

dt.DefaultView.RowFilter = "FolderName , DocumentName ASC";
dt = dt.DefaultView.ToTable();
Share:
50,696
user1931665
Author by

user1931665

Updated on August 12, 2020

Comments

  • user1931665
    user1931665 over 3 years

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

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

    How to alphabetically sort DocumentName based on FolderName in .Net Framework 2.0.

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

    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;
    
     }
    
  • user1931665
    user1931665 over 11 years
    Thanks a lot, it worked. For a simple line of coding, i wrote a hell of stupid code
  • André Silva
    André Silva over 11 years
    Don't forget to upvote and mark as correct answer. That estimulates us to help more and more.
  • Neel
    Neel about 10 years
    stange this ans is useful for OP bt still havnt marked as asnwer
  • André Silva
    André Silva about 10 years
    Yeah. I get a bit sad when this happens, but at least I know that it helped OP.
  • Admin
    Admin almost 8 years
    @AndréSilva your solution is sorting only those columns which are of string type, suppose i have a column say colA which is having string values like 1.23, 11.34, and 2.34. Then it will sort it as 1.23, 11.34, 2.34 which is wrong.
  • André Silva
    André Silva almost 8 years
    @PranayDeep SaQiB managed to answer that in another post. Basically you need to duplicate the DataTable and change the column you have into the type you need in order to sort it. Take a look at his answer: stackoverflow.com/questions/7572685/…
  • Greg Trevellick
    Greg Trevellick almost 4 years
    Thanks - this is the only answer that worked for me. The key was having DataTable OFI= viewFI.ToTable(); which most other answers omitted.