Thursday, March 22, 2012

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"> 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" <ren@.spumco.com> wrote in message
news:slrndn9k0i.nr0.ren@.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"
>
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" <ren@.spumco.com> wrote in message
news:slrndn9k0i.nr0.ren@.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"
>
On Fri, 11 Nov 2005 Nitin <signitin@.gmail.com> wrote:
> you need to have two loops one for Rows and another nested loop for column
s.
> 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" <ren@.spumco.com> wrote in message
> news:slrndn9k0i.nr0.ren@.carbon.redbrick.dcu.ie...
>
"I hear ma train a comin'
... hear freedom comin"
> 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 ser born every minute.
- Dr. "Happy" Harry Cox
"Stimp" <ren@.spumco.com> wrote in message
news:slrndn9p35.r0p.ren@.carbon.redbrick.dcu.ie...
> On Fri, 11 Nov 2005 Nitin <signitin@.gmail.com> wrote:
> hmmm.. ok I'll try to continue with hackin away at it.
> Databinding isn't an option since I need to transpose the data
>
>
> --
> "I hear ma train a comin'
> ... hear freedom comin"
>
On Sat, 12 Nov 2005 Kevin Spencer <kevin@.DIESPAMMERSDIEtakempis.com> wrote:
> Of course Databinding is an option. Just transpose the data into a bindabl
e
> 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"

0 comments:

Post a Comment