By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

HTML Table with Dynamic Columns

P: n/a
Hello

Does anyone know how to display data from a data source such as an
array or SqlDataReader or XML etc within an HTML table whose number of
columns can change according to the value of a variable ?

This is one thing that XSL is brilliant at but I can't find a nice
..Net solution

The following is how it can easily be achieved in XSL.

-------- XML file -------------

<root>
<letter>a</letter>
<letter>b</letter>
<letter>d</letter>
<letter>c</letter>
<letter>f</letter>
<letter>e</letter>
<letter>g</letter>
<letter>h</letter>
<letter>i</letter>
</root>

------- XSL file --------------

<xsl:template match="root">
<xsl:variable name="columns" select="6" />
<TABLE>
<xsl:for-each select="letter[position() mod $columns = 1]">
<TR>
<xsl:for-each select=".|following-sibling::letter[position()
&lt; $columns]">
<TD>
<xsl:value-of select="." />
</TD>
</xsl:for-each>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In .NET 1.1, you could load the XML into a DataSet and then bind the DataSet
to a DataGrid (which will get rendered as a HTML table).
In ASP.NET 2.0, there is an XmlDataSource control which makes your job even
easier.

But if you don't want to use the DataGrid in .NET 1.1, you could write a few
lines of code to write out a table by iterating through the DataRows in the
first DataTable of the DataSet.

Mujtaba.

"Fresh Air Rider" <Fr*************@Hotmail.com> wrote in message
news:55**************************@posting.google.c om...
Hello

Does anyone know how to display data from a data source such as an
array or SqlDataReader or XML etc within an HTML table whose number of
columns can change according to the value of a variable ?

This is one thing that XSL is brilliant at but I can't find a nice
.Net solution

The following is how it can easily be achieved in XSL.

-------- XML file -------------

<root>
<letter>a</letter>
<letter>b</letter>
<letter>d</letter>
<letter>c</letter>
<letter>f</letter>
<letter>e</letter>
<letter>g</letter>
<letter>h</letter>
<letter>i</letter>
</root>

------- XSL file --------------

<xsl:template match="root">
<xsl:variable name="columns" select="6" />
<TABLE>
<xsl:for-each select="letter[position() mod $columns = 1]">
<TR>
<xsl:for-each select=".|following-sibling::letter[position()
&lt; $columns]">
<TD>
<xsl:value-of select="." />
</TD>
</xsl:for-each>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.