DataTable 中的数据,行转列显示

private DataTable RowToCol(DataTable Dt_newData)
{
DataTable Dt_Return = new DataTable();
DataColumn col = new DataColumn();
col.ColumnName = "Title";
col.Caption = "Title";
Dt_Return.Columns.Add(col);


List colName = new List();

foreach (DataColumn dc in Dt_NewData.Columns) //(DataColumn dc in Dt_NewData.Columns)
{

Dt_Return.Rows.Add(dc.ColumnName);

}


int c = 0;
for (int n = 0; n < Dt_NewData.Rows.Count; n++)//DataRow dr in Dt_NewData.Rows
{
c = c + 1;
col = new DataColumn();
col.DataType = typeof(string);
col.ColumnName = "cols" + c;
col.Caption = "cols " + c;
Dt_Return.Columns.Add(col);


// 全部显示
for (int m = 0; m < Dt_NewData.Columns.Count; m++)
{

Dt_Return.Rows[m][col.ColumnName] = Dt_NewData.Rows[n][m].ToString();

// Dt_Return.Rows[m][n] = Dt_NewData.Rows[n][m].ToString();

}

}

return Dt_Return;

}

转载于:https://www.cnblogs.com/aran/archive/2012/09/14/2684299.html

你可能感兴趣的:(DataTable 中的数据,行转列显示)