Sorting rows in a data table
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.
Related videos on Youtube
vidya sagar
Updated on July 08, 2022Comments
-
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 onCOL2
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 theDataTable
itself, not theDataView
. -
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 over 10 yearsi want the value ascending in terms of price value which is decimal. how to do it?
-
Steam over 10 yearsThis approach seems fine. But is there no direct way of doing it ? Why don't they have a DataTable.sort("by") ?
-
WSBT over 9 yearsThanks. It's worth noting that, "occr desc" here, "occr" is the column name, "desc" means "descending".
-
ivg over 9 yearsVidya 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 over 9 yearsThe 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 almost 9 years1) You must create new table
DataTable sortedDT = new DataTable()
. 2) You need to useImportRow
(you can't add row from different table) -
Sai over 8 yearsThis worked for me dataTable.DefaultView.Sort = "Col1, Col2, Col3". Little clean code.
-
مسعود almost 8 yearsIt 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 over 7 yearsJust like @Sai, you can modify the DataTable.DefaultView.Sort directly. No need to "break out" the view and recreate a table.
-
Johnny Bones about 7 years@Sai is the MAN! That little line of code just saved my sanity!
-
Aleksei almost 7 yearsThanks 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 almost 7 yearsGreat minds think alike. I was about to post the same solution after reading @JayR's.
-
Thameem over 4 yearsfor Column_name cuz i was confused what was occr in Jay Riggs's solution :)
-
Subarata Talukder over 4 yearsIt 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 about 4 yearsNote 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 almost 4 yearsWonderful and easy solution :)
-
ZF007 over 3 yearsadd context so we can learn and to prevent down-voting or removal of question. EoR.
-
kenmtb over 3 yearsThis is a fantastic answer. Allows the use of dynamically generated text strings for filtering and sorting! Glad I found this answer!