G
Guest
I need to transform a data table that would eventually be exported, E.g.
The original table is as follows: ID fName lName
1 Alex Smith
2 Joe May
I need it in this "format": ID 1 2
fName Alex Joe
lName Smith May
The column names of the transformed table don't matter.
I have the following code that sort of does it:
private DataTable TransformTable(DataTable dt)
{
DataTable dtnew = new DataTable();
// Add the columns.
for (int i = 0; i <= dt.Rows.Count ; i++)
{
dtnew.Columns.Add(dt.Columns.ColumnName);
}
// Add the rows.
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
DataRow dr = dtnew.NewRow();
dtnew.Rows.Add(dr);
}
// Add the data.
for (int r=0; r<= dt.Rows.Count - 1; r++)
{
for (int c=0; c<= dt.Columns.Count - 1; c++)
{
dtnew.Rows[c][r] = dt.Rows[r][c].ToString();
}
}
return dtnew;
}
I just can't get how to add the column names as field values.
Thanks
Alex
The original table is as follows: ID fName lName
1 Alex Smith
2 Joe May
I need it in this "format": ID 1 2
fName Alex Joe
lName Smith May
The column names of the transformed table don't matter.
I have the following code that sort of does it:
private DataTable TransformTable(DataTable dt)
{
DataTable dtnew = new DataTable();
// Add the columns.
for (int i = 0; i <= dt.Rows.Count ; i++)
{
dtnew.Columns.Add(dt.Columns.ColumnName);
}
// Add the rows.
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
DataRow dr = dtnew.NewRow();
dtnew.Rows.Add(dr);
}
// Add the data.
for (int r=0; r<= dt.Rows.Count - 1; r++)
{
for (int c=0; c<= dt.Columns.Count - 1; c++)
{
dtnew.Rows[c][r] = dt.Rows[r][c].ToString();
}
}
return dtnew;
}
I just can't get how to add the column names as field values.
Thanks
Alex