471,829 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,829 software developers and data experts.

looping through a datatable

This is a question I'm carrying over from a previous one I made today
since I've simplified where the problem is...

I have a datatable, tblFeatures, which has around 30 columns (one for
each 'feature').

I also have between 1 and 3 rows of data (one for each 'vehicle').

I want to transpose this table so that I ouput the rows horizontally
and the columns vertically.

I'm looping through the datatable and manually creating the HTML as I
progress.. e.g.

Dim colFeatures As DataColumn
Dim rowFeatures As DataRow

Dim sOutputFeatures As System.Text.StringBuilder = New
System.Text.StringBuilder

For Each colFeatures In tblFeatures.Columns

sOutputFeatures.Append("<TR>")
sOutputFeatures.Append("<TD>" & colFeatures.ColumnName & "</TD>")

For Each rowFeatures In tblFeatures.Rows

sOutputFeatures.Append("<TD>" & rowFeatures(colFeatures) & "</TD>")
Next

sOutputFeatures.Append("</TR>")
Next

Response.Write sOutputFeatures.ToString()
but for some reason .. this is outputting 29 empty <TD></TD> for each
row value that it outputs.

I think I may be using the wrong syntax around the:
'For Each rowFeatures In tblFeatures.Rows' for loop

Does anyone know the proper syntax I should be using here?

Thanks in advance!
Peter
--

"I hear ma train a comin'
.... hear freedom comin"

Nov 19 '05 #1
5 3287
> Does anyone know the proper syntax I should be using here?

Maybe DataBinding?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.

"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
This is a question I'm carrying over from a previous one I made today
since I've simplified where the problem is...

I have a datatable, tblFeatures, which has around 30 columns (one for
each 'feature').

I also have between 1 and 3 rows of data (one for each 'vehicle').

I want to transpose this table so that I ouput the rows horizontally
and the columns vertically.

I'm looping through the datatable and manually creating the HTML as I
progress.. e.g.

Dim colFeatures As DataColumn
Dim rowFeatures As DataRow

Dim sOutputFeatures As System.Text.StringBuilder = New
System.Text.StringBuilder

For Each colFeatures In tblFeatures.Columns

sOutputFeatures.Append("<TR>")
sOutputFeatures.Append("<TD>" & colFeatures.ColumnName & "</TD>")

For Each rowFeatures In tblFeatures.Rows

sOutputFeatures.Append("<TD>" & rowFeatures(colFeatures) & "</TD>")
Next

sOutputFeatures.Append("</TR>")
Next

Response.Write sOutputFeatures.ToString()
but for some reason .. this is outputting 29 empty <TD></TD> for each
row value that it outputs.

I think I may be using the wrong syntax around the:
'For Each rowFeatures In tblFeatures.Rows' for loop

Does anyone know the proper syntax I should be using here?

Thanks in advance!
Peter
--

"I hear ma train a comin'
... hear freedom comin"

Nov 19 '05 #2
you need to have two loops one for Rows and another nested loop for columns.
Still databinding is the best option you can use datagrid or datalist(
datagridview in case of asp.net 2.0)
regards
Nitin
"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
This is a question I'm carrying over from a previous one I made today
since I've simplified where the problem is...

I have a datatable, tblFeatures, which has around 30 columns (one for
each 'feature').

I also have between 1 and 3 rows of data (one for each 'vehicle').

I want to transpose this table so that I ouput the rows horizontally
and the columns vertically.

I'm looping through the datatable and manually creating the HTML as I
progress.. e.g.

Dim colFeatures As DataColumn
Dim rowFeatures As DataRow

Dim sOutputFeatures As System.Text.StringBuilder = New
System.Text.StringBuilder

For Each colFeatures In tblFeatures.Columns

sOutputFeatures.Append("<TR>")
sOutputFeatures.Append("<TD>" & colFeatures.ColumnName & "</TD>")

For Each rowFeatures In tblFeatures.Rows

sOutputFeatures.Append("<TD>" & rowFeatures(colFeatures) & "</TD>")
Next

sOutputFeatures.Append("</TR>")
Next

Response.Write sOutputFeatures.ToString()
but for some reason .. this is outputting 29 empty <TD></TD> for each
row value that it outputs.

I think I may be using the wrong syntax around the:
'For Each rowFeatures In tblFeatures.Rows' for loop

Does anyone know the proper syntax I should be using here?

Thanks in advance!
Peter
--

"I hear ma train a comin'
... hear freedom comin"

Nov 19 '05 #3
On Fri, 11 Nov 2005 Nitin <si******@gmail.com> wrote:
you need to have two loops one for Rows and another nested loop for columns.
Still databinding is the best option you can use datagrid or datalist(
datagridview in case of asp.net 2.0)
hmmm.. ok I'll try to continue with hackin away at it.

Databinding isn't an option since I need to transpose the data

regards
Nitin
"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
This is a question I'm carrying over from a previous one I made today
since I've simplified where the problem is...

I have a datatable, tblFeatures, which has around 30 columns (one for
each 'feature').

I also have between 1 and 3 rows of data (one for each 'vehicle').

I want to transpose this table so that I ouput the rows horizontally
and the columns vertically.

I'm looping through the datatable and manually creating the HTML as I
progress.. e.g.

Dim colFeatures As DataColumn
Dim rowFeatures As DataRow

Dim sOutputFeatures As System.Text.StringBuilder = New
System.Text.StringBuilder

For Each colFeatures In tblFeatures.Columns

sOutputFeatures.Append("<TR>")
sOutputFeatures.Append("<TD>" & colFeatures.ColumnName & "</TD>")

For Each rowFeatures In tblFeatures.Rows

sOutputFeatures.Append("<TD>" & rowFeatures(colFeatures) & "</TD>")
Next

sOutputFeatures.Append("</TR>")
Next

Response.Write sOutputFeatures.ToString()
but for some reason .. this is outputting 29 empty <TD></TD> for each
row value that it outputs.

I think I may be using the wrong syntax around the:
'For Each rowFeatures In tblFeatures.Rows' for loop

Does anyone know the proper syntax I should be using here?

Thanks in advance!
Peter
--

"I hear ma train a comin'
... hear freedom comin"


--

"I hear ma train a comin'
.... hear freedom comin"

Nov 19 '05 #4
> Databinding isn't an option since I need to transpose the data

Of course Databinding is an option. Just transpose the data into a bindable
object and databind to that.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox

"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
On Fri, 11 Nov 2005 Nitin <si******@gmail.com> wrote:
you need to have two loops one for Rows and another nested loop for
columns.
Still databinding is the best option you can use datagrid or datalist(
datagridview in case of asp.net 2.0)


hmmm.. ok I'll try to continue with hackin away at it.

Databinding isn't an option since I need to transpose the data

regards
Nitin
"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
This is a question I'm carrying over from a previous one I made today
since I've simplified where the problem is...

I have a datatable, tblFeatures, which has around 30 columns (one for
each 'feature').

I also have between 1 and 3 rows of data (one for each 'vehicle').

I want to transpose this table so that I ouput the rows horizontally
and the columns vertically.

I'm looping through the datatable and manually creating the HTML as I
progress.. e.g.

Dim colFeatures As DataColumn
Dim rowFeatures As DataRow

Dim sOutputFeatures As System.Text.StringBuilder = New
System.Text.StringBuilder

For Each colFeatures In tblFeatures.Columns

sOutputFeatures.Append("<TR>")
sOutputFeatures.Append("<TD>" & colFeatures.ColumnName & "</TD>")

For Each rowFeatures In tblFeatures.Rows

sOutputFeatures.Append("<TD>" & rowFeatures(colFeatures) & "</TD>")
Next

sOutputFeatures.Append("</TR>")
Next

Response.Write sOutputFeatures.ToString()
but for some reason .. this is outputting 29 empty <TD></TD> for each
row value that it outputs.

I think I may be using the wrong syntax around the:
'For Each rowFeatures In tblFeatures.Rows' for loop

Does anyone know the proper syntax I should be using here?

Thanks in advance!
Peter
--

"I hear ma train a comin'
... hear freedom comin"


--

"I hear ma train a comin'
... hear freedom comin"

Nov 19 '05 #5
On Sat, 12 Nov 2005 Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
Databinding isn't an option since I need to transpose the data


Of course Databinding is an option. Just transpose the data into a bindable
object and databind to that.


nah.. I've used the following code and it's working perfectly..

For Each colFeatures In tblFeatures.Columns

sOutputFeatures.Append("<TR>")
sOutputFeatures.Append("<TD>" & colFeatures.ColumnName & "</TD>")

For Each rowFeatures In tblFeatures.Rows

If Not rowFeatures(colFeatures) Is DBNull.Value Then
sOutputFeatures.Append("<TD>" &
rowFeatures(colFeatures) & "</TD>")
End If
Next

sOutputFeatures.Append("</TR>")
Next
cheers!
--

"I hear ma train a comin'
.... hear freedom comin"

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Kelvin | last post: by
7 posts views Thread by Ken | last post: by
2 posts views Thread by =?Utf-8?B?Sm9iIExvdA==?= | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.