views:

411

answers:

3

I'm having trouble creating a Gridview that can span 2 rows for each record.
I've tried using a templateField with headerTemplate and itemTemplates as separate tables, but I can't get them to format correctly.

Does anybody know of an easier solution for this problem?

+1  A: 

Have you considered using a Repeater instead of a GridView? The Repeater is probably the easiest way to have 2 rows per record, you get more control over the tabular markup that gets generated e.g.

<asp:Repeater runat="server" ID="Repeater1" >
<HeaderTemplate>
    <table>
        <tr>
            <th>Field 1</th>
        </tr>
        <tr>
            <th>Field 2</th>
        </tr>
</HeaderTemplate>
<ItemTemplate>
    <tr>
        <td><%# DataBinder.Eval(Container.DataItem, "field1") %></td>
    </tr>
    <tr>
        <td><%# DataBinder.Eval(Container.DataItem, "field2") %></td>
    </tr>
</ItemTemplate>
<FooterTemplate>
    </table>
</FooterTemplate>
</asp:Repeater>
PhilPursglove
+4  A: 

Yeah, don't use the GridView. A GridView is used to display tabular data. What you want doesn't fit that criteria. You could use a repeater however my preference is the new in 3.5 ListView

    <asp:ListView ID="ListView1" runat="server"  >
  <ItemTemplate>
    <tr>
      <td>
        <%# Eval("Something") %>
      </td>        
    </tr>
    <tr>
  <td>
  <%# Eval("SomethingElse") %>
  </td>
 </tr>
  </ItemTemplate>

  <EmptyDataTemplate>
    <div>
          No data was returned.</td>
      </div>        
  </EmptyDataTemplate> 
  <LayoutTemplate>    
            <asp:PlaceHolder ID="itemPlaceholder" runat="server">  
  </LayoutTemplate> 
</asp:ListView>
Kevin
In addition to this, in asp.net 4.0 you wont need the <LayoutTemplate> part for the ListView, making it even more straightforward.
DavidGouge
The list view works great. Thank you very much. The only thing left for me to do is sort by the column headings.
Steve McCall
A: 

Thanks guys, I'll have a look at these solutions and report back.

Steve McCall