Reverse rows in a DataTable? RRS feed

  • Question

  • User-2013806645 posted
    Is there a function in C# that will reverse the rows in a DataTable? For example, if row 1 has the value of ONE, 2 has TWO, and row 3 has a value of THREE, is there a function that will reverse this and change the value of row 1 to THREE, 2 to TWO and row 3 to value ONE. I'm not experienced enough (i don't think) to write some kind of bubble sort for a datatable and was just wondering if C# has one prebuild. Thanks ~
    Wednesday, October 1, 2003 10:52 PM

All replies

  • User645477409 posted
    DataView has built-in sort. Check this. Does this help?
    Thursday, October 2, 2003 2:52 PM
  • User889652929 posted


    I created this method:

            // Reverse row order
            private DataTable ReverseDataTable(DataTable originalDT)
                // create return datatable with the same structure of the original
                DataTable reversedDT = originalDT.Clone();
    // iterate backwards, adding each row to the return DataTable for (int i = originalDT.Rows.Count - 1; i >= 0; i--) { var row = originalDT.Rows[i]; reversedDT.ImportRow(row); } reversedDT.AcceptChanges(); return reversedDT; }

    Monday, December 7, 2020 12:53 PM