Transform DataTables

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
 
G

Guest

Unfortunately we're still running .net 1.1 for the next 6 months at least.

Cor Ligthert said:
Alex,

In version 2005 the overloaded DataView.ToTable does this much easier,

http://msdn2.microsoft.com/en-us/library/wec2b2e6.aspx

I hope this helps,

Cor

Alex said:
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

 
C

Cor Ligthert [MVP]

Alex,

I don't like this one

http://www.vb-tips.com/dbpages.aspx?ID=67b889fc-8593-4fcb-be92-fabbdffba809

I hope this helps,

Cor

Alex said:
Unfortunately we're still running .net 1.1 for the next 6 months at least.

Cor Ligthert said:
Alex,

In version 2005 the overloaded DataView.ToTable does this much easier,

http://msdn2.microsoft.com/en-us/library/wec2b2e6.aspx

I hope this helps,

Cor

Alex said:
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

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top