views:

401

answers:

2

I have a datagrid where some of the text needs to span multiple columns. Here is an example of what I need.

Row # Image Name Price Date

1 xxx My Name $99 1/1/2009 xxx xxx Long description goes here

2 xxx name 2 $99 1/1/2009 xxx
xxx Another long description

Is something like this possible in Asp.Net using a datagrid? Any suggestions on how to do this?

+1  A: 

You might find it easier to use an ASP.Net Repeater instead, and have multiple rows per DataItem. That way, you get complete control over the layout.

It's technically possible to use the ASP.Net GridView to do it, but I suspect this wouldn't be the most elegant solution here. The GridView (and Datagrid) were designed out of the box for one row per DataItem.

Here is some sample ASP.Net containing a Repeater that illustrates an approach that might work for you:

        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table>
        </HeaderTemplate>
        <tr>
            <th>
                Col 1
            </th>
            <th>
                Col 2
            </th>
            <th>
                Col 3
            </th>
        </tr>
        <ItemTemplate>
            <tr>
                <td>
                    <%# Eval("Field1") %>
                </td>
                <td>
                    <%# Eval("Field2") %>
                </td>
                <td>
                    <%# Eval("Field3") %>
                </td>
            </tr>
            <tr>
                <td>
                    <%# Eval("Field4") %>
                </td>
                <td colspan="2">
                    <%# Eval("Field5") %>
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>

Note that the ItemTemplate contains two HTML table rows, the second of which contains a td with a colspan of 2.

Hope that helps a little.

Scott Ferguson
+1 to you too - This, also, would be a better control for this situation than a DataGrid.
David Stratton
Thanks. Went with the repeater and customized it a bit to work with what I needed.
doclove
+1  A: 

Do you have to use a DataGrid?, I would suggest the ListView control. It has all the functionality you need and uses templates for full UI control.

rick schott
+1 - This would be a better control for this situation than a DataGrid.
David Stratton
This would work also. I personally find the ListView control a little clunky for displaying tabular data though, and prefer the Repeater. You do get more out of the box (richer event model etc) with a ListView however.
Scott Ferguson
I only recommend it if you need the functionality, otherwise a Repeater will do. Most people using that use a DataGrid or GirdView need more than display capability.
rick schott