GridView inside GridView

以下代码演示gridView嵌套:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridViewInsideGridView.WebForm1" %>

 

<!DOCTYPE html>

 

<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="SateName" HeaderText="StateName" HeaderStyle-HorizontalAlign="Left" />

 

                                            </Columns>

 

                                        </asp:GridView>

 

                                    </div>

 

                                </td>

 

                            </tr>

 

                        </ItemTemplate>

 

                    </asp:TemplateField>

 

                </Columns>

 

            </asp:GridView>

 

        </div>

 

    </form>

 

</body>

 

</html>

using System;

 

using System.Collections.Generic;

 

using System.Data;

 

using System.Data.SqlClient;

 

using System.Linq;

 

using System.Web;

 

using System.Web.UI;

 

using System.Web.UI.WebControls;

 

namespace GridViewInsideGridView

 

{

    public partial class WebForm1 : System.Web.UI.Page

 

    {

        SqlConnection con = new SqlConnection(@"Data Source=(localdb)\Projects;Initial Catalog=DemoDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");

 

        protected void Page_Load(object sender, EventArgs e)

 

        {

            if (!IsPostBack)

 

            {

                BindGridview();

            }

        }

        // This method is used to bind gridview from database

 

        protected void BindGridview()

 

        {

            con.Open();

            SqlCommand cmd = new SqlCommand("select TOP 4 CountryId,CountryName from Country", con);

 

            SqlDataAdapter da = new SqlDataAdapter(cmd);

 

            DataSet ds = new DataSet();

 

            da.Fill(ds);

            con.Close();

            gvParentGrid.DataSource = ds;

            gvParentGrid.DataBind();

 

 

 

        }

        protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)

 

        {

            if (e.Row.RowType == DataControlRowType.DataRow)

 

            {

                con.Open();

                GridView gv = (GridView)e.Row.FindControl("gvChildGrid");

 

                int CountryId = Convert.ToInt32(e.Row.Cells[1].Text);

 

                SqlCommand cmd = new SqlCommand("select * from State where CountryID=" + CountryId, con);

 

                SqlDataAdapter da = new SqlDataAdapter(cmd);

 

                DataSet ds = new DataSet();

 

                da.Fill(ds);

                con.Close();

                gv.DataSource = ds;

                gv.DataBind();

            }

        }

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值