Introduction
This article and code snippet here shows how to bind a repeater control and Gridview control to a relational data so the GridView and the Repeater control show related data.
Inserting a Repeater within a Gridview is easy but to bind the headline in the GridViewand data items corresponding to the particular head line into a Repeater control is a little bit trickier. According to a requirement, I have to bind the company name as headline and the top four news of the company as its data items under the headline, then again the next company name and top four news of the respective company and so on.
Mechanism
Take a GridView control and set its AutoGenerateColumns="False". Put a label control within the GridView to bind company name. You can also use its BoundField.
Now, within the same ItemTemplate, place a repeater control and put one more label control within this repeater control to bind news of respective company. If you are using different ItemTemplate, then it will bind in another column, otherwise it will bind in the same column but in different rows as shown in fig. below.
Now it is your choice, whether you are interested to bind in same or different column. Here, I have used div tag, to separate company and their news rows. The div tag gives one more advantage i.e. we can apply CSS to a particular div for richer UI.
The complete source code is here.
<div style="width:400px; font-family:Arial; font-size:small">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div style="color:Blue;font-weight:bold">
<asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"compname") %>'></asp:Label>
</div>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%#DataBinder.Eval(Container, "DataItem.InnerVal") %>'>
<ItemTemplate>
<div style="padding-left:20px; padding-top:5px">
<asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"news") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<div style="text-align:right">
<asp:HyperLink ID="link" runat="server">More</asp:HyperLink>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>

Nitin jainPosted Apr 4, 2012, 10:11 AM
I ahappy to get idea fron this articvle ,can i add more rows into the repeater which is already in side the gridview, For example i can add row into the grid view as well as into the repeater. Waiting for your reply.. [email protected]
Sean FullmanPosted Jan 20, 2009, 7:20 PM
Your row databound code works great for the gridview, but what if I want to drill into the repeater. For example, I want to conditionally set the navigate url property of a hyperlink in the repeater. Thanks