Tuesday, November 17, 2009

Nesting Gridview in Repeater

Once I have tried to nest Gridview in a Repeater, you might wondering why we require this.

The scenario was like that. Grid view has some limitations. Main thing is we can show the data in columns. But my rquirement was showing the first row as a heading.

As follows

-------------------Heading A category name from a query----------------------
-------------------Details about that category from the same query-----------
-------------------Details about that category from the same query-----------
-------------------Details about that category from the same query-----------
-------------------Details about that category from the same query-----------
-------------------Details about that category from the same query-----------
Slno name description Date
---- ------------ ------------------ -----------------
---- ------------ ------------------ -----------------
---- ------------ ------------------ -----------------
---- ------------ ------------------ -----------------

I hope the above example can give an outline about the scenario. I went for repeater with gridview.


<asp:UpdatePanel ID="updPanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Repeater ID="rptid" runat="server" OnItemDataBound="rptid_RowDataBound" EnableViewState="false">
<ItemTemplate>
<table cellpadding="0" cellspacing="0" width="90%">
<tr>
<td>
 
</td>
</tr>
<tr>
<td id="tdid" class="style" align="left" runat="server" title="Click Here to expand/Collapse">
<asp:Label ID="lblCategory" CssClass="Header" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"CategoryID")%>' EnableViewState="false"></asp:Label>
<asp:HiddenField id="hidCategory" runat="server" value='<%#DataBinder.Eval(Container.DataItem," CategoryID")%>'/>
</td>
</tr>
<tr>
<td>
 
</td>
</tr>
<tr>
<tr>
<td>
<asp:GridView ID="grdDetails" runat="server" AllowPaging="false" EmptyDataText="No Data Available" SkinID="skinid " EnableViewState="false" Width="100%" style="display:none">
<Columns>
<asp:BoundField DataField="AgreementName" HeaderText=" Award Agreement Name" />
---------------------------Bind Code goes here-------------------------------
---------------------------Bind Code goes here-------------------------------
---------------------------Bind Code goes here-------------------------------
---------------------------Bind Code goes here-------------------------------
---------------------------Bind Code goes here-------------------------------
---------------------------Bind Code goes here-------------------------------
---------------------------Bind Code goes here-------------------------------
---------------------------Bind Code goes here-------------------------------
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
 
</td>
</tr>
</ItemTemplate>
</asp:Repeater>

In code behind we need to use gridview from the rowdatabound of repeater itself as shown below.
protected void rptid_RowDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
---------------------------Bind Code goes here-------------------------------
---------------------------Bind Code goes here-------------------------------
GridView grdDetails = (GridView)e.Item.FindControl("grdDetails");
---------------------------Bind Code goes here-------------------------------
}

No comments:

Post a Comment

...

Obstacles are those frightful things you see when you take your eyes off your goal.------> by Henry Ford