C# DataTable 行转列

时间:2024-01-01 12:27:39
   #region 根据datatable获得列名
/// <summary>
/// 根据datatable获得列名
/// </summary>
/// <param name="dt">表对象</param>
/// <returns>返回结果的数据列数组</returns>
public static string[] GetColumnsByDataTable(DataTable dt)
{
string[] strColumns = null; if (dt.Columns.Count > )
{
int columnNum = ;
columnNum = dt.Columns.Count;
strColumns = new string[columnNum];
for (int i = ; i < dt.Columns.Count; i++)
{
strColumns[i] = dt.Columns[i].ColumnName;
}
}
return strColumns;
}
#endregion
  for (int i = ; i < name.Length; i++)
{
dtColumn.Rows.Add(new string[] { "", name[i], "" });
}

搞定!