DataTable行列转置

DataTable dtNew = new DataTable();
        dtNew.Columns.Add("ColumnName", typeof(string));
        for (int i = 0; i < dt.Rows.Count; i++)
       {
            dtNew.Columns.Add("Column" + (i + 1).ToString(), typeof(string));
       }
        foreach (DataColumn dc in dt.Columns)
        {
            DataRow drNew = dtNew.NewRow();
            drNew["ColumnName"] = dc.ColumnName;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                drNew[i + 1] = dt.Rows[i][dc].ToString();
            }
            dtNew.Rows.Add(drNew);
        }

 

转载于:https://www.cnblogs.com/zhaogaojian/p/10527119.html

你可能感兴趣的:(DataTable行列转置)