Sorting rows in a data table

543,347

Solution 1

I'm afraid you can't easily do an in-place sort of a DataTable like it sounds like you want to do.

What you can do is create a new DataTable from a DataView that you create from your original DataTable. Apply whatever sorts and/or filters you want on the DataView and then create a new DataTable from the DataView using the DataView.ToTable method:

   DataView dv = ft.DefaultView;
   dv.Sort = "occr desc";
   DataTable sortedDT = dv.ToTable();

Solution 2

This will help you...

DataTable dt = new DataTable();         
dt.DefaultView.Sort = "Column_name desc";
dt = dt.DefaultView.ToTable();

Solution 3

Its Simple Use .Select function.

DataRow[] foundRows=table.Select("Date = '1/31/1979' or OrderID = 2", "CompanyName ASC");
DataTable dt = foundRows.CopyToDataTable();

And it's done......Happy Coding

Solution 4

Maybe the following can help:

DataRow[] dataRows = table.Select().OrderBy(u => u["EmailId"]).ToArray();

Here, you can use other Lambda expression queries too.

Solution 5

Did you try using the Select(filterExpression, sortOrder) method on DataTable? See here for an example. Note this method will not sort the data table in place, if that is what you are looking for, but it will return a sorted array of rows without using a data view.

Share:
543,347

Related videos on Youtube

vidya sagar
Author by

vidya sagar

Updated on July 08, 2022

Comments

  • vidya sagar
    vidya sagar almost 2 years

    We have two columns in a DataTable, like so:

    COL1   COL2
    Abc    5
    Def    8
    Ghi    3
    

    We're trying to sort this datatable based on COL2 in decreasing order.

    COL1            COL2
    ghi             8
    abc             4
    def             3
    jkl             1
    

    We tried this:

    ft.DefaultView.Sort = "COL2 desc";
    ft = ft.DefaultView.ToTable(true);
    

    but, without using a DataView, we want to sort the DataTable itself, not the DataView.

  • Gustavo Mori
    Gustavo Mori over 12 years
    @vidyasagar No problem. Also, for future reference, if an answer is valuable, you should vote it up (example, mine?). And if an answer is "THE" answer, you should mark it as the answer (example, Jay's).
  • Ranjith Kumar Nagiri
    Ranjith Kumar Nagiri over 10 years
    i want the value ascending in terms of price value which is decimal. how to do it?
  • Steam
    Steam over 10 years
    This approach seems fine. But is there no direct way of doing it ? Why don't they have a DataTable.sort("by") ?
  • WSBT
    WSBT over 9 years
    Thanks. It's worth noting that, "occr desc" here, "occr" is the column name, "desc" means "descending".
  • ivg
    ivg over 9 years
    Vidya wants to sort his table by occr in desc order. Which the simple code above does. It does exactly what Jay Riggs (accepted answer) showed except this is done in one line of code.
  • ΩmegaMan
    ΩmegaMan over 9 years
    The suggestion was to better the post; in the future place that information about the code into the response. For it betters the chance of someone upvoting the post or even selecting it as the answer.
  • marbel82
    marbel82 almost 9 years
    1) You must create new table DataTable sortedDT = new DataTable(). 2) You need to use ImportRow (you can't add row from different table)
  • Sai
    Sai over 8 years
    This worked for me dataTable.DefaultView.Sort = "Col1, Col2, Col3". Little clean code.
  • مسعود
    مسعود almost 8 years
    It works for sorting but If I want to replace my original table with the sorted one what should I do. So that when I access this table the second time it is still sorted.
  • Jonny
    Jonny over 7 years
    Just like @Sai, you can modify the DataTable.DefaultView.Sort directly. No need to "break out" the view and recreate a table.
  • Johnny Bones
    Johnny Bones about 7 years
    @Sai is the MAN! That little line of code just saved my sanity!
  • Aleksei
    Aleksei almost 7 years
    Thanks for the answer. Your way #1 helped in my case: I got a very special IComparer defined, so to use it I did something like this: DataRow[] rows = dt.Rows.Cast<DataRow>().OrderBy(row => row.Field<string>("FIELD_NAME"), MyCustomComparer.Instance).ToArray();
  • Drew Chapin
    Drew Chapin almost 7 years
    Great minds think alike. I was about to post the same solution after reading @JayR's.
  • Thameem
    Thameem over 4 years
    for Column_name cuz i was confused what was occr in Jay Riggs's solution :)
  • Subarata Talukder
    Subarata Talukder over 4 years
    It works fine for me. If you need more benefit then you may initialize data to a session variable: "Session["nameTable"] = yourDataTable" and then use: "DataTable dt = (DataTable)Session["nameTable"]". Finally use above sorting code using "dt" DataTable instance. Thanks
  • Tawab Wakil
    Tawab Wakil about 4 years
    Note that if, like OP, you are only interested in the sorting aspect of this and don't want to filter the results, you can specify it like this: Select("", "CompanyName ASC").
  • M. Fawad Surosh
    M. Fawad Surosh almost 4 years
    Wonderful and easy solution :)
  • ZF007
    ZF007 over 3 years
    add context so we can learn and to prevent down-voting or removal of question. EoR.
  • kenmtb
    kenmtb over 3 years
    This is a fantastic answer. Allows the use of dynamically generated text strings for filtering and sorting! Glad I found this answer!