行列转换

              在做项目的时候用到这个了,公司的Sean老大帮我写了这个。记录下来.
            int colLength = dt.Rows.Count;
            int rowLength = dt.Columns.Count;
            DataRow[] newRows = new DataRow[rowLength];
            DataTable subDT = new DataTable();

            for (int i = 0; i < colLength; i++)
            {
                subDT.Columns.Add("C_" + (i + 1).ToString());
                for (int j = 0; j < rowLength; j++)
                {
                    if (newRows[j] == null) newRows[j] = subDT.NewRow();
                    newRows[j][i] = dt.Rows[i][j];
                }
            }
            foreach (DataRow row in newRows)
            {
                subDT.Rows.Add(row);
            }

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