Gridview Inside a GridView

Description:
Here I am showing Gridview as Child Control in GridView.Write the following in aspx page


Default.aspx:

<asp:GridView runat="server" ID="gvEmpdetails" DataKeyNames="EmpID" DataSourceID="EmpDetails"

AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnRowDataBound="gvEmpdetails_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkhead" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression="EmpID" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" SortExpression="EmpSal" />
<asp:BoundField DataField="EmpBranch" HeaderText="EmpBranch" SortExpression="EmpBranch" />
<asp:BoundField DataField="EmpCode" HeaderText="EmpCode" SortExpression="EmpCode" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView runat="server" ID="gvEmpChilddetails"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression="EmpID" />
<asp:BoundField DataField="EmpTax" HeaderText="EmpTax" SortExpression="EmpTax" />
<asp:BoundField DataField="EmpPF" HeaderText="EmpPF" SortExpression="EmpPF" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Default.aspx.cs:

First identify the childgridview id in the RowDataboundEvent and then bind the data accordingly based on the condition.Write the below code in the RowDataBoundEvent as follows.

protected void gvEmpdetails_RowDataBound(object sender, GridViewRowEventArgs e)

    {
      
    
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            rowindex = e.Row.RowIndex - 1;
            if (gvEmpdetails.Rows.Count > 0)
            {
                  Empid = Convert.ToInt16(gvEmpdetails.DataKeys[e.Row.RowIndex-1].Value);
            }
            if (Empid > 0)
            {
                //TemplateField temp = (TemplateField)e.Row.
                GridView gdChildDetails =       (GridView)gvEmpdetails.Rows[rowindex].FindControl("gvEmpChilddetails");
                SqlConnection scon = null;
                SqlDataAdapter sda = null;
                DataSet ds = null;
                scon = new SqlConnection(@"Data Source=TAPA-PC;user Id=sa;password=123;DataBase=Customer");
                sda = new SqlDataAdapter("select * from EmpDeduction where EmpID=" + Empid, scon);
                ds = new DataSet();
                sda.Fill(ds, "emp");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    gdChildDetails.DataSourceID = null;
                    gdChildDetails.DataSource = ds;
                    gdChildDetails.DataBind();
                }
                else
                {
                    gdChildDetails.DataSourceID = null;
                    gdChildDetails.DataSource = null;
                    gdChildDetails.DataBind();
                }
            }

        }





0 Responses to “Gridview Inside a GridView”

Post a Comment

Labels

Topics