【问题标题】:Transpose a datatable转置数据表
【发布时间】:2015-11-29 23:03:46
【问题描述】:

我有下面的数据表。我想更改数据表以将数据表转换为旁边的数据表。我该怎么做?

我想这样做的原因是因为 excel 文件将数据带入如图所示,然后我想在第 1 行对这些数据进行排序

当前表

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

新数据表

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

【问题讨论】:

  • 欢迎来到 Stack Overflow!请向我们展示您的尝试。
  • 我真的找不到任何可以让我这样做的东西
  • @user2061088 因为你不应该这样做
  • 他们希望我按第 1 行从高到低对列进行排序,但找不到这样做的方法
  • @user2061088 仅用于打印目的?

标签: c# datatable transpose


【解决方案1】:

.NET 不包含转置数据表的方法。你必须自己做。 This 网站上有一个关于示例转置方法的教程。下面我复制粘贴代码sn-p:

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

【讨论】:

  • 上面的代码你测试了吗? DataTable 没有基础对象的 Add 方法,正如您在 foreach 循环中所尝试的那样。
【解决方案3】:

创建一个新表,其维度与您之前的维度相反,并将之前的列分配给新行,将旧行分配给新列。

例如:

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

列表继续,假设您来自

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

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

【讨论】:

  • 如果可以建表,可以赋值enw和old变量。创建表时,只需在新位置使用旧变量即可。我不懂 C#,但我在 BASICjavac 的修改版本中做过这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-21
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
相关资源
最近更新 更多