Asp.Net

Tuesday, 3 September 2013

Nested Gridview

aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="nestedgridview.aspx.cs" Inherits="nestedgridview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Gridview within Gridivew - Nested gridview example in asp.net </title>
    <script language="javascript" type="text/javascript">
        function divexpandcollapse(divname) {
            var div = document.getElementById(divname);
            var img = document.getElementById('img' + divname);
            if (div.style.display == "none") {
                div.style.display = "inline";
                img.src = "minus.gif";
            } else {
                div.style.display = "none";
                img.src = "plus.gif";
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvParentGrid" runat="server" DataKeyNames="countryid" Width="300"
            AutoGenerateColumns="false" OnRowDataBound="gvUserInfo_RowDataBound" GridLines="None"
            BorderStyle="Solid" BorderWidth="1px" BorderColor="#df5015">
            <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
            <RowStyle BackColor="#E1E1E1" />
            <AlternatingRowStyle BackColor="White" />
            <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
            <Columns>
                <asp:TemplateField ItemStyle-Width="20px">
                    <ItemTemplate>
                        <a href="JavaScript:divexpandcollapse('div<%# Eval("countryid") %>');">
                            <img id="imgdiv<%# Eval("countryid") %>" width="9px" border="0" src="plus.gif" />
                        </a>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="countryid" HeaderText="CountryId" HeaderStyle-HorizontalAlign="Left" />
                <asp:BoundField DataField="countryname" HeaderText="CountryName" HeaderStyle-HorizontalAlign="Left" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <tr>
                            <td colspan="100%">
                                <div id="div<%# Eval("countryid") %>" style="display: none; position: relative; left: 15px;
                                    overflow: auto">
                                    <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Double"
                                        BorderColor="#df5015" GridLines="None" Width="250px">
                                        <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                        <RowStyle BackColor="#E1E1E1" />
                                        <AlternatingRowStyle BackColor="White" />
                                        <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                        <Columns>
                                            <asp:BoundField DataField="stateid" HeaderText="StateID" HeaderStyle-HorizontalAlign="Left" />
                                            <asp:BoundField DataField="statename" HeaderText="StateName" HeaderStyle-HorizontalAlign="Left" />
                                        </Columns>
                                    </asp:GridView>
                                </div>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

No comments:

Post a Comment