//创建 表
DataTable tables = new DataTable();
//添加 创建 列
//第一列
DataColumn cums = new DataColumn();
cums.ColumnName = "UserName";
cums.DataType = typeof(string);
tables.Columns.Add(cums);
cums = new DataColumn();
cums.ColumnName = "Age";
cums.DataType = typeof(string);
tables.Columns.Add(cums);
tables.Columns.Add("Address", typeof(string)); 这种用的较多
tables.Columns["Address"].SetOrdinal(0); //修改DataTable 列的顺序
string[] columnNames = GetColumnsByDataTable(tables);
string ss = columnNames[0];
/// <summary>
/// 根据datatable获得列名
/// </summary>
/// <param name="dt">表对象</param>
/// <returns>返回结果的数据列数组</returns>
public string[] GetColumnsByDataTable(DataTable dt)
{
string[] strColumns = null;
if (dt.Columns.Count > 0)
{
int columnNum = 0;
columnNum = dt.Columns.Count;
strColumns = new string[columnNum];
for (int i = 0; i < dt.Columns.Count; i++)
{
strColumns[i] = dt.Columns[i].ColumnName;
}
}
return strColumns;
}