27

I have a DataTable that looks like below;

|              ID                    | ItemIndex |   ItemValue
ce895bd9-9a92-44bd-8d79-986f991154a9     1            3
ae7d714e-a457-41a8-8bb4-b5a0471c3d2f     2            2
a774dff3-acc0-4f50-a211-a775e28dcae3     2            1
292bbd50-290b-4511-9e4e-2e74e3ebe273     3            2
ae7d714e-a457-41a8-8bb3-b5a0471c3d22     3            1

I want to sort this table by ItemIndex first, then sort the sorted table by ItemValue.

How can I achieve this?

Edit: after sorting, I want my table like below;

|              ID                    | ItemIndex |   ItemValue
ce895bd9-9a92-44bd-8d79-986f991154a9     1            3
a774dff3-acc0-4f50-a211-a775e28dcae3     2            1
ae7d714e-a457-41a8-8bb4-b5a0471c3d2f     2            2
ae7d714e-a457-41a8-8bb3-b5a0471c3d22     3            1
292bbd50-290b-4511-9e4e-2e74e3ebe273     3            2
2
  • How can I group table and sort each item seperately? May 29, 2013 at 7:30
  • 2
    DataView dv = new DataView(dt); dv.Sort = "ItemIndex, ItemValue";`
    – user1838179
    Nov 26, 2013 at 8:00

7 Answers 7

45

You can use LINQ to DataSet/DataTable

var newDataTable = yourtable.AsEnumerable()
                   .OrderBy(r=> r.Field<int>("ItemIndex"))
                   .ThenBy(r=> r.Field<int>("ItemValue"))  
                   .CopyToDataTable();
3
  • 1
    Do we now have two DataTable objects?
    – DOK
    Apr 30, 2013 at 14:57
  • 2
    @DOK, yes but the result can be assigned back to original DataTable.
    – Habib
    Apr 30, 2013 at 14:57
  • How can I group table and sort each item seperately? May 29, 2013 at 7:31
32

Create a DataView and use the Sort Property:

DataView dv = new DataView(dt);
dv.Sort = "ItemIndex, ItemValue";

e.g.

foreach (DataRowView row in dv) {
   Console.WriteLine(" {0} \t {1}", row["ItemIndex"], row["ItemValue"]);
}

For more information, check out MDSN for a more thorough example:

http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx

4
  • Short and sweet, doesn't create another (potentially large) object, and this code is highly readable, if slightly less sexy than using Linq.
    – DOK
    Apr 30, 2013 at 15:03
  • thanks, it's working great also. But I didn't understand whether dt is sorted table after dv.Sort. Apr 30, 2013 at 15:10
  • @George - Hi, this is working for string columns, but i have a column say ColA. with string values 1.23, 11.45 and 2.34, as these are string values your way of sorting will have not effect on them. I am stuck here what should i do?
    – user6102644
    May 12, 2016 at 12:31
  • @user612644 I realize this is an old comment to an even older answer. Your problem appears to be the age old "How to sort numbers stored as strings?" problem. That is a completely different problem of a completely different scope. Apr 26, 2017 at 22:04
25

On the datatable object, just get the defaultview object and set the sort.

dataTable.DefaultView.Sort = "ItemIndex, ItemValue";
3
  • This is the simplest answer so far and works perfectly, thank you.
    – Taegost
    Oct 13, 2014 at 13:01
  • 3
    For clarification: You still need to either call dataTable.DefaultView.ToTable() in order to actually perform the sort and copy the (sorted) rows to a new DataTable, or you can iterate the dataTable.DefaultView. But you can't simply apply the .Sort property and then iterate the rows of the original dataTable and expect them to be in sorted order.
    – Mr. T
    Jun 24, 2015 at 15:29
  • 1
    ToTable() does not need to be called for data binding. I believe Default view is used internally.
    – Ty Petrice
    Jun 26, 2015 at 3:34
12

By creating dataview

var dataView = new DataView(dataTable);
dataView.Sort = "ItemIndex ASC, ItemValue ASC"

Here dataTable is table you want to sort

1
3

Alternatively you can use that

DataView oDataSet;
oDataSet.Tables[0].DefaultView.Sort = "Column1 ASC ";
1
  • How would we go about doing that with multiple columns? Please check the question title.
    – specstr
    May 11, 2020 at 11:12
3

Here is my take, given the helpful comments of others here.

DataView dataView = new DataView(dataTable);//datatable to dataview
dataView.Sort = "columnName1 ASC, columnName2 DESC";//string that contains the column name  followed by "ASC" (ascending) or "DESC" (descending)
dataTable = dataView.ToTable();//push the chages back to the datatable
1
  • you deserve more than 1 thumbs up!!!. this was the ONLY solution that worked for me. I purposely set the original querys to have different sorts. EVERY other method the rows were not always in the desired order. this one was perfect!!! (sorted on 12 columns) I was comparing two datasets to see if the rows were the same. The purpose was to see if a stored proc change had messed up the data. THIS DESERVES MORE VOTES!!!! THank you !!! .ToTable() rocks!!!!
    – roblem
    Oct 23, 2023 at 13:58
1
_UserAuditTrailTable.DefaultView.Sort = sortExpression;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.