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

 

posted @ 2019-03-13 22:30  zhaogaojian  阅读(900)  评论(0编辑  收藏  举报