This code is useful to transpose the datatable rows and columns.
DataTable dtSource = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
// Bind the source datatable from the database
dtSource = dataSource();
}
public DataTable FlipDataTable(GridView gvNew)
{
DataTable dataTable = new DataTable("dataTable");
// create column
DataRow newRow;
for (int iRow = 0; iRow < dtSource.Columns.Count; iRow++)
{
newRow = dataTable.NewRow();
newRow[0] = dtSource.Columns[iRow].ToString();
for (int iCol = 1; iCol <= dtSource.Rows.Count; iCol++)
{
newRow[iCol] = dtSource.Rows[iCol - 1][iRow];
}
dataTable.Rows.Add(newRow);
}
}