GridViews – Multiple Rows Per Record
I don’t know about you, but I’ve had several occasions where I’ve needed to use the simplicity of the GridView control in ASP.NET. DataBinding, Paging, Sorting, etc. But I’ve also wanted to stack the information for a record in multiple rows.
Most of the time this happens when I need to display data that would take up two or three times as much of the width as my layout will allow.
The solution to this problem is quite simple: Use templated columns and Literal controls.
Assuming you are starting off with a GridView control that renders like this:
| Header1 | Header2 | Header3 | Header4 | Header5 |
| Data1 | Data2 | Data3 | Data4 | Data5 |
| etc… |
But we want to display the data as:
| Header1 | Header2 | Header3 |
| Header4 | Header5 | |
| Data1 | Data2 | Data3 |
| Data4 | Data5 | |
| etc… | ||
To get your GridView to display like this, you will need to:
- Turn Header3 and Header4 into a templated columns.
- Add the following code into the TemplatedField section for column 3
<HeaderTemplate> <asp:Label ID="Label3" runat="server" Text="Header3"></asp:Label> <asp:Literal ID="Literal1" runat="server" Text="</td></tr><tr><td colspan="2">"></asp:Literal> <asp:Label ID="Label4" runat="server" Text="Header4"></asp:Label> </HeaderTemplate>
- You’ll want to modify the ItemTemplate and EditItemTemplate so that they have similar Literal tags and add the appropriate control after it for what WAS in column 4.
That’s all there is to it. Admittedly, there is a bit more work here than just drag and drop, but it beats having to write all of the databinding, paging and sorting code by hand.
Other Related Items:
Don't Mess With Me, I am a Programmer - T-Shirt (40 colors)Cotton T-Shirt 5.6 oz., heavyweight 100% preshrunk cotton T-shirt, Quarter-turned. Seamless collar, Taped shoulder-to-shoulder, Double-needle stitched neck, sleeves and hemmed bottom. Direct printed on Front of T-Shirt, this is not a transfer. Won't peel or crack in washer/dryer.
All Purpose NetThey are specifically designed for koi, surface skimming, and general all-purpose use. Heavy duty aluminum construction, telescoping handle to 6-1/2 . Soft fish safe nylon netting. Sure-grip handle. Aluminum & Nylon Mesh.
ULRIKE Oblong China Silk Scarf With Beads - A rich texture of multiple reds trimmed on each end with a row of red mother-of-pearl stars.Spontaneously painting the dyes free-hand and skillfully manipulating their bleeds and edges as they merge with the silk, Ulrike achieves uniquely soft, multi-shaded images in which, unlike printed silk, colors are equally rich on both sides of the scarf.










[...] GridViews – Multiple Rows Per Record (Dave M. Bush) [...]