user123 user123 - 2 months ago 12
ASP.NET (C#) Question

Dynamic datatable take Reverse order in asp.net

In my web application, I have created

DataTable dt= new DataTable();
dt.Columns.Add("Month");


and made fill the column name "Month" with date as

Month(Column name)
oct-2014
Nov-2014
Dec-2014
jan-2015
feb-2015
mar-2015
apr-2015
May-2015
June-2015
July-2015
Aug-2015
Sept-2015
Oct-2015
Nov-2015
Dec-2015
Jan-2016
Feb-2016
March-2016
April-2016
May-2016
June-2016
July-2016
Aug-2016


and made fill my dynamic table

Now, i want this dynamic table data to be Reverse order

for eg:

Month(Column name)
Aug-2016
July-2016
June-2016
May-2016
April-2016
March-2016
Feb-2016
Jan-2016
Dec-2015
Nov-2015
......
Nov-2014
oct-2014


like this i need to display dynamic data table is first row is last and last row is first.

Thank you.

Answer

Try this.

  DataTable reversedDt = new DataTable();
  reversedDt = dt.Clone();
  for (var row = dt.Rows.Count - 1; row >= 0; row--)
         reversedDt .ImportRow(dt.Rows[row]);
Comments