Wednesday 25 July 2012

How to implement Nested Gridview (Gridview within a gridview)



In Design page:

 <asp:GridView CssClass="content" ID="GVDistrict" runat="server" Width="100%" HorizontalAlign="Left" DataKeyNames="DISTRICT"
 AutoGenerateColumns="False" CaptionAlign="Left" OnRowDataBound="GVDistrict_OnRowDataBound"   >
 <Columns>
 <asp:BoundField DataField="DISTRICT" HeaderText="District">
     <HeaderStyle Width="12%" />
 </asp:BoundField>
   <asp:TemplateField HeaderText="Category Details">
 <ItemTemplate>

 <asp:GridView CssClass="content" ID="CategoryDeatils" runat="server" Width="100%" CaptionAlign="Left"
  AutoGenerateColumns="False" AllowSorting="True" HorizontalAlign="Left" >
  <columns>
   <asp:BoundField Datafield="PKG_CATEGORY" HeaderText="Category" />
    <asp:BoundField Datafield="CASES" HeaderText="#Cases" />
     <asp:BoundField Datafield="AMT" HeaderText="Package Amt." />     
    <asp:BoundField Datafield="TOTALAPPAMOUNT" HeaderText="Total App. Amt."/>
  </columns>
  <HeaderStyle CssClass="headerContent" HorizontalAlign="Left" />
 <AlternatingRowStyle HorizontalAlign="Left" BackColor="GhostWhite" />
 </asp:GridView>


 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 <HeaderStyle CssClass="headerContent" HorizontalAlign="Left" />
 <AlternatingRowStyle HorizontalAlign="Left" BackColor="GhostWhite" />
 </asp:GridView>




In code behind:

protected void bindparentgridview()

            
    {       

                  try
                  {

                    // Get some data in datatable and bind it to parent gridview


                    GVDistrict.DataSource = Gdt;
                    GVDistrict.DataBind();
                  }
                   
                
                   catch (HttpException ex)
                 {
                   Lblmsg.Text = "There seems to be problem in Network Connectivity. Please try again." + ex.ToString();
                 }
                   catch (Exception ex)
                 {
                   Lblmsg.Text = "There seems to be an Exception.  !!" + ex.ToString();
                 }

             
    }




 protected void GVDistrict_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {



        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           
           // get childgridview control here

           GridView ChildGridview = ((GridView)e.Row.FindControl("CategoryDeatils"));


          
            try
            {

              // get some datatable dt and bind the childgridview here

               
                ChildGridview.DataSource = dt;
                ChildGridview.DataBind();


            }
            catch (HttpException ex)
            {
                Lblmsg.Text = "There seems to be problem in Network Connectivity. Please try again." +    ex.ToString();
            }
            catch (Exception ex)
            {
                Lblmsg.Text = "There seems to be an Exception.  !!" + ex.ToString();
            }


        }

    }



No comments:

Post a Comment