This code is useful to transpose the datatable rows and columns.

   1: DataTable dtSource = new DataTable();
   2: protected void Page_Load(object sender, EventArgs e)
   3:    {
   4:     // Bind the source datatable from the database
   5:     dtSource = dataSource();
   6:    }
   7:  
   8: public DataTable FlipDataTable(GridView gvNew)
   9:     {
  10:         DataTable dataTable = new DataTable("dataTable");
  11:  
  12:         // create column
  13:         DataRow newRow;
  14:         for (int iRow = 0; iRow < dtSource.Columns.Count; iRow++)
  15:         {
  16:             newRow = dataTable.NewRow();
  17:  
  18:             newRow[0] = dtSource.Columns[iRow].ToString();
  19:  
  20:             for (int iCol = 1; iCol <= dtSource.Rows.Count; iCol++)
  21:             {
  22:                 newRow[iCol] = dtSource.Rows[iCol - 1][iRow];
  23:             }
  24:  
  25:             dataTable.Rows.Add(newRow);
  26:         }
  27:     }