views:

163

answers:

2

I have a GridView bound to some XML data that looks like this:

<Root>
    <Column1>
        <Item1 type="l1style">Item 1</Item1>
        <Item2 type="l2style">Item 2</Item2>
        <Item3 type="l3style">Item 3</Item3>
    </Column1>

    <Column2>
        <Item4 type="l1style">Item 4</Item4>
        <Item5 type="l2style">Item 5</Item5>
    </Column2>

    <Column3>
        <Item6 type="l1style">Item 6</Item6>
        <Item7 type="l2style">Item 7</Item7>
    </Column3>
</Root>

In some cases, though, the Column3 node isn't there.

I'd like to render something like:

<table>
    <thead>
        <tr>
            <th scope="col">Column1</th>
            <th scope="col">Column2</th>
            <th scope="col">Column3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>                
                <ul>
                    <li class="l1style">Item 1</li>
                    <li class="l2style">Item 2</li>
                    <li class="l3style">Item 3</li>
                </ul>
            </td>
            <td>
                <ul>
                    <li class="l1style">Item 4</li>
                    <li class="l2style">Item 5</li>
                </ul>
            </td>
            <td>
                <ul>
                    <li class="l1style">Item 6</li>
                    <li class="l2style">Item 7</li>
                </ul>
            </td>
        </tr>
    </tbody>
</table>

How can a Repeater control be used inside of a GridView, or is there a better way to accomplish this? Thanks.

+1  A: 

That looks like html that could be emitted by a repeater or datalist, without a gridvew.

If you use a gridview, you'd put a repeater in an itemtemplate in a templatefield column. You'd have to bind something to the gridview so that it renders one row for the repeater to show.

<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            Your Repeater

I think a repeater alone would do what you want.

Steve
If I put a Repeater in a TemplateField, how can I set its data source to be the current column (Column 1, 2 and [sometimes] 3)?
Bullines
You could use <%# Bind("fieldname") %> in say a literal, but I'm not so sure this is the best way of going about this. Is it a big problem to replace the gridview with a repeater? A repeater is alot simpler than a gridview and is lightweight and faster.
Steve
A: 

There's also a problem with the html you're showing as you're expected output. You're not delcaring any columns in the rows using <td> that will match to your columns headers <th>.

You should be outputting something like:

<table>
 <tr>
   <th> ... </th>
   <th> ... </th>
   <th> ... </th>
 </tr>
 <tr>
   <td> ... </td>
   <td> ... </td>
   <td> ... </td>
 </tr>
...

Steve is right in that you can achieve this with a repeater. Simply declare a header template to hold the opening table tag and the heading row, a footer template to hold the closing tag for the table, and an item template to output the, guess what, the item lines.

You could also try using xml transformation using somthing called an xsl stylesheet. You load your xml, apply the transformation and hey presto nicely formatted html. Try googling for some examples. I'll pop back here again later when I have more time and drop an example if you're not having any luck.

pete's a hutt