10

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;

 }
1
  • 1
    You mention this information is obtained from the Database so why dont you do the ORDER BY clause in the SQL Query? Otherwise have you tried using a DataView rather than a DataTable? Commented Dec 27, 2012 at 9:25

5 Answers 5

42
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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot, it worked. For a simple line of coding, i wrote a hell of stupid code
Don't forget to upvote and mark as correct answer. That estimulates us to help more and more.
stange this ans is useful for OP bt still havnt marked as asnwer
Yeah. I get a bit sad when this happens, but at least I know that it helped OP.
@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.
|
3

Here was my solution to this problem:

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

1 Comment

Thanks - this is the only answer that worked for me. The key was having DataTable OFI= viewFI.ToTable(); which most other answers omitted.
2

Have you tried DataView.Sort?

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

Comments

0

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

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

Alternatively check this :

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

thanks

Comments

0

You can use

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.