Nested GridView in ASp.net C#

Posted by Unknown On Friday, 15 March 2013 1 comments
Following is the .aspx file code write this first. . .
<script type="text/javascript">
        function showNestedGridView(obj) {
            var nestedGridView = document.getElementById(obj);
            var imageID = document.getElementById('image' + obj);

            if (nestedGridView.style.display == "none") {
                nestedGridView.style.display = "inline";
                imageID.src = "minus.png";
            } else {
                nestedGridView.style.display = "none";
                imageID.src = "plus.png";
            }
        }
</script>
<br />
<br />
<br />
<center>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
        DataKeyNames="pid" ShowFooter="True"
        onrowdatabound="GridView1_RowDataBound"
        onrowcommand="GridView1_RowCommand">
        <Columns>


            <asp:TemplateField>
                <ItemTemplate>
                    <%--<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/pimage/plus-icon.png" Width="10px" Height="10px" CommandName="Show" CommandArgument='<%#Eval("pid") %>' />--%>
                     <a href="javascript:showNestedGridView('customerID-<%# Eval("pid") %>');">
                                <img id="imagecustomerID-<%# Eval("pid") %>" alt="Click to show/hide record" border="0" src="plus.png" />
                    </a>
                </ItemTemplate>
            </asp:TemplateField>
            <%--<asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID="Labid" runat="server" Text="Product Id"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Labid" runat="server" Text='<%#Eval("pid") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>--%>
            <asp:TemplateField HeaderText="Product Name" SortExpression="pname">
                <%--<HeaderTemplate>
                    <asp:Label ID="Label5" runat="server" Text="Product Name"></asp:Label>
                </HeaderTemplate>--%>
                <ItemTemplate>
                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("pid") %>'></asp:HiddenField>
                    <asp:Label ID="Label6" runat="server" Text='<%#Eval("pname") %>'></asp:Label>
                </ItemTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Description" SortExpression="pdesc">
               <%-- <HeaderTemplate>
                    <asp:Label ID="Label7" runat="server" Text="Description"></asp:Label>
                </HeaderTemplate>--%>
                <ItemTemplate>
                    <asp:Label ID="Label8" runat="server" Text='<%#Eval("pdesc") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
                       <asp:TemplateField HeaderText="Total">
                <ItemTemplate>
                    <asp:Label ID="Labtot" runat="server" Text='<%#Eval("ptot") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="Labgtot" runat="server" Text="Label"></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
                <asp:TemplateField>
                <ItemTemplate>
                    <tr>
                                <td colspan="100%">
                                   <div id="customerID-<%# Eval("pid") %>" style="display:none;position:relative;left:25px;" >
                         <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField HeaderText="Product ID">
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%#Eval("pid") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Quantity">
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("qty") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Rate">
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%#Eval("rate") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                    </div>
                    </td>
                    </tr>         
                   
                </ItemTemplate>
            </asp:TemplateField>
           
        </Columns>
    </asp:GridView>

Now Apply follow code in .aspx.cs file of row databound event. . .

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        con.Close();
       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string sValue = ((HiddenField)e.Row.FindControl("HiddenField1")).Value;
            GridView gridViewNested = (GridView)e.Row.FindControl("GridView2");
            con.Open();
            SqlDataAdapter adp = new SqlDataAdapter("select * from task8_qty where pid='" + sValue + "'", con);
            DataTable dtnew = new DataTable();
            adp.Fill(dtnew);
            gridViewNested.DataSource = dtnew;
            gridViewNested.DataBind();
           
            con.Close();

        }

1 comments:

Anonymous said...

Very simple example. Easy to understand.
Thank you.

Post a Comment