Transpose a datatable

跟風遠走 提交于 2019-11-28 12:11:39

问题


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

回答1:


.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;
}



回答2:


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);
}



回答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]


来源:https://stackoverflow.com/questions/14817061/transpose-a-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!