Thursday, September 24, 2009

Reverse Data Table

This Method is used the Reverse the given input DataTable

For Eample,

Input Data Table has the following Records,

NAME TITLE DEV_IN SCOST DCOST SOLD
-------- -------------------- -------- ---------- ---------- ----------
RAMESH HOTEL MANAGEMENT DBASE 12000 35000 4
RAMESH DEAD LEE PASCAL 99.95 4500 73

public static DataTable ReverseDataTable(DataTable dataTable)
{
DataTable table = new DataTable();
DataRow[] rowArray = dataTable.Select();
table = dataTable.Clone();
for (int i = rowArray.Length - 1; i >= 0; i--)
{
table.ImportRow(rowArray[i]);
}
return table;
}

OutPut

NAME TITLE DEV_IN SCOST DCOST SOLD
-------- -------------------- -------- ---------- ---------- ----------
RAMESH DEAD LEE PASCAL 99.95 4500 73
RAMESH HOTEL MANAGEMENT DBASE 12000 35000 4

3 comments:

Unknown said...

Does not really work...
update as follows :


DataTable table = new DataTable();
//DataRow[] rowArray = dataTable.Select();
table = dataTable.Clone();
for (int i = dataTable.Rows.Count - 1; i >= 0; i--)
{
table.ImportRow(dataTable.Rows[i]);
}
return table;

SampathPrabu said...
This comment has been removed by the author.
SampathPrabu said...

let me know what issue faced by using above code.