C# DataTable 行转列
作者:互联网
行转列
public System.Data.DataTable RowToCol(System.Data.DataTable dataSource)
{
var tblPivot = new System.Data.DataTable();
var firstColumnName = dataSource.Columns[0].ColumnName;
tblPivot.Columns.Add(firstColumnName);
for (int i = 1; i < dataSource.Rows.Count; i++)
{
tblPivot.Columns.Add(Convert.ToString(i));
}
for (int col = 0; col < dataSource.Columns.Count; col++)
{
var r = tblPivot.NewRow();
r[0] = dataSource.Columns[col].ToString();
for (int j = 1; j < dataSource.Rows.Count; j++)
r[j] = dataSource.Rows[j][col];
tblPivot.Rows.Add(r);
}
tblPivot.Columns.Remove(firstColumnName);
return tblPivot;
}
标签:Rows,C#,col,转列,dataSource,tblPivot,DataTable,Columns 来源: https://www.cnblogs.com/wen593692967/p/15612054.html