looping through a datatable

S

Stimp

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
 
K

Kevin Spencer

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.
 
N

Nitin

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
 
S

Stimp

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
 
K

Kevin Spencer

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
 
S

Stimp

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!
 

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

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top