DataTable行列转换

时间:2021-05-12 17:07:35
 #region DataTable行列转换
        /// <summary>
        /// DataTable行列转换
        /// </summary>
        /// <param name="scr">要转换的DataTable</param>
        /// <param name="columnIndex">要作为Column的那列</param>
        /// <returns></returns>
        public static DataTable DataTableColToRow(DataTable src,int columnIndex)
        {
            DataTable result = new DataTable();
            DataColumn column = src.Columns[columnIndex];
            result.Columns.Add(column.ColumnName);
            ; i < src.Rows.Count; i++)
            {
                result.Columns.Add(src.Rows[i][column].ToString());
            }

            foreach (DataColumn col in src.Columns)
            {
                if(col==column)
                {
                    continue;
                }

                ];
                newRow[] = col.ColumnName;
                ; i < src.Rows.Count; i++)
                {
                    newRow[i + ] = src.Rows[i][col];
                }
                result.Rows.Add(newRow);
            }
            return result;
        }

        /// <summary>
        ///  DataTable行列转换
        /// </summary>
        /// <param name="src">要转换的DataTable</param>
        /// <param name="columnName">要作为Column的那列</param>
        /// <returns></returns>
        public static DataTable DataTableColToRow(DataTable src,string columnName)
        {
            ; i < src.Columns.Count; i++)
            {
                if(src.Columns[i].ColumnName.ToUpper()==columnName.ToUpper())
                {
                    return DataTableColToRow(src, i);
                }
            }
            return new DataTable();
        }

        #endregion