DataTable行列转置

时间:2022-03-31 12:29:13
DataTable dtNew = new DataTable();
dtNew.Columns.Add("ColumnName", typeof(string));
for (int i = ; i < dt.Rows.Count; i++)
{
dtNew.Columns.Add("Column" + (i + ).ToString(), typeof(string));
}
foreach (DataColumn dc in dt.Columns)
{
DataRow drNew = dtNew.NewRow();
drNew["ColumnName"] = dc.ColumnName;
for (int i = ; i < dt.Rows.Count; i++)
{
drNew[i + ] = dt.Rows[i][dc].ToString();
}
dtNew.Rows.Add(drNew);
}