GridView双层嵌套

前台代码:

<div>

        <asp:GridView ID="GridViewOne" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridViewOne_RowDataBound">

            <Columns>

                <asp:TemplateField>

                    <EditItemTemplate>

                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DepartName") %>'></asp:TextBox>

                    </EditItemTemplate>

                    <ItemTemplate>

                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("DepartName") %>'></asp:Label>

                        <br />

                        <asp:GridView ID="GridViewTwo" runat="server">

                        </asp:GridView>

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>

    </div>

后台代码:

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

    {

        StaffInfoBLL sbll = new StaffInfoBLL();

        DepartmentInfoBLL dbll = new DepartmentInfoBLL();

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack) {

                InitGridViewOne();

            }

        }



        private void InitGridViewOne()

        {

            this.GridViewOne.DataSource = dbll.GetAll();

            this.GridViewOne.DataBind();

        }



        protected void GridViewOne_RowDataBound(object sender, GridViewRowEventArgs e)

        {

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

                GridView g = e.Row.FindControl("GridViewTwo") as GridView;

                DepartmentInfo d = e.Row.DataItem as DepartmentInfo;

                g.DataSource = sbll.GetByDepartId(d.DepartId);

                g.DataBind();

            }

        }

    }

  

你可能感兴趣的:(GridView)