2

I have the data table below. I would like to change the data table to transform the data table into the one next to it. How can I do this?

The reason why i would like o do this is because the excel file brings the data in like shown and then i would like to sort this data out on Row 1

Current table

      || Col 1 || Col 2 || Col 3
____________________________________    
Row 1 || 10    ||  20   || 30
Row 2 || 1     ||  2    || 3 

New Datatable

      || Row 1 || Row 2 
Col 1 ||  10   ||   1
Col 2 ||  20   ||   2
Col 3 ||  30   ||   3
Andrej Adamenko
  • 1,650
  • 15
  • 31
user2061088
  • 33
  • 1
  • 1
  • 7

3 Answers3

11

.NET does not include a method to transpose data tables. You have to make your own. This website has a tutorial on an example transpose method. I will copy and paste the code snippet below:

private DataTable Transpose(DataTable dt)
{
    DataTable dtNew = new DataTable();

    //adding columns    
    for(int i=0; i<=dt.Rows.Count; i++)
    {
       dtNew.Columns.Add(i.ToString());
    }



    //Changing Column Captions: 
    dtNew.Columns[0].ColumnName = " ";

     for(int i=0; i<dt.Rows.Count; i++) 
     {
      //For dateTime columns use like below
       dtNew.Columns[i+1].ColumnName = Convert.ToDateTime(dt.Rows[i].ItemArray[0].ToString()).ToString("MM/dd/yyyy");
      //Else just assign the ItermArry[0] to the columnName prooperty
     }

    //Adding Row Data
    for(int k=1; k<dt.Columns.Count; k++)
    {
        DataRow r = dtNew.NewRow();
        r[0] = dt.Columns[k].ToString(); 
        for(int j=1; j<=dt.Rows.Count; j++)
        r[j] = dt.Rows[j-1][k];  
        dtNew.Rows.Add(r);
    }

 return dtNew;
}
Lexi Tramel
  • 447
  • 5
  • 17
0
DataTable newDt = new DataTable();
//Select indexes
var indexes = dt.Rows.Cast<DataRow>().Select(row => dt.Rows.IndexOf(row));
//Select the columns
var newCols = indexes.Select(i => "Row" + i);
//Add columns
foreach(var newCol in newCols)
{
  newDt.Add(newCol);
}
//Select rows
var newRows = dt.Rows.Cast<DataColumn>().Select(col =>
                                                  {
                                                    row = newDt.NewRow();
                                                    foreach(int i in indexes)
                                                    {
                                                      row[i] = dt.Rows[i][col.Name];
                                                    }
                                                    return row;
                                                  });
//Add row to new datatable
foreach(var row in newRows)
{
  newDt.Add(row);
}
LukeHennerley
  • 6,344
  • 1
  • 32
  • 50
  • Did you test the above code? DataTable has no Add method off the base object, as attempted in your foreach loops. – Jazimov Jul 28 '20 at 20:42
-3

Create a new table, with reversed dimensions from what you previously had, and assign the previous columns to the new rows, and the old rows to the new columns.

eg:

oldCol1 -> newRow1
oldCol2 -> newRow2
oldCol3 -> newRow3
oldCol4 -> newRow4
oldRow1 -> newCol1
oldCol2 -> newCol2
oldvar[a][b] -> newvar[b][a]

and the list goes on, assumming that you are going from

col1 || col2 || col3 || col4
row1 ||[a][b]||[c][d]||[e][f]
row2 ||[g][h]||[i][j]||[k][l]

to

row1 || col1 || col2
row2 ||[a][b]||[g][h]
row3 ||[c][d]||[i][j]
row4 ||[e][f]||[k][l]
Azulflame
  • 1,534
  • 2
  • 15
  • 30
  • If you can set up a table, you can assign enw and old variables. Just use the old variables in the new spot when creating the table. I don't know C#, but I've done this in `BASIC`, `java` and a modified version of `c` – Azulflame Feb 11 '13 at 17:19