Please refer below code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country")
});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
ReverseDataTableColumns(dt);
}
private DataTable ReverseDataTableColumns(DataTable sourceDataTable)
{
if(sourceDataTable.Rows.Count > 0)
{
DataTable dtReversed = new DataTable();
var se = sourceDataTable.Columns.Cast<DataColumn>().Reverse().ToArray();
dtReversed.Columns.AddRange(sourceDataTable.Columns.Cast<DataColumn>().Reverse().Select(x => new DataColumn(x.ColumnName)).ToArray());
for (int j = 0; j < sourceDataTable.Rows.Cast<DataRow>().Select(x => x.ItemArray.Reverse()).Count(); j++)
{
dtReversed.Rows.Add();
var row = sourceDataTable.Rows.Cast<DataRow>().Select(x => x.ItemArray.Reverse()).ElementAt(j);
for (int k = 0; k < row.Count(); k++)
{
dtReversed.Rows[j][k] = row.ElementAt(k);
}
}
return dtReversed;
}else
{
return sourceDataTable;
}
}
VB
Protected Sub Page_Load(sender As Object, e As EventArgs)
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
ReverseDataTableColumns(dt)
End Sub
Private Function ReverseDataTableColumns(sourceDataTable As DataTable) As DataTable
If sourceDataTable.Rows.Count > 0 Then
Dim dtReversed As New DataTable()
Dim se = sourceDataTable.Columns.Cast(Of DataColumn)().Reverse().ToArray()
dtReversed.Columns.AddRange(sourceDataTable.Columns.Cast(Of DataColumn)().Reverse().[Select](Function(x) New DataColumn(x.ColumnName)).ToArray())
For j As Integer = 0 To sourceDataTable.Rows.Cast(Of DataRow)().[Select](Function(x) x.ItemArray.Reverse()).Count() - 1
dtReversed.Rows.Add()
Dim row = sourceDataTable.Rows.Cast(Of DataRow)().[Select](Function(x) x.ItemArray.Reverse()).ElementAt(j)
For k As Integer = 0 To row.Count() - 1
dtReversed.Rows(j)(k) = row.ElementAt(k)
Next
Next
Return dtReversed
Else
Return sourceDataTable
End If
End Function
I hope this will help you out