嵌套gridview的问题asp.net/C#

时间:2011-03-30 11:05:39

标签: asp.net gridview

我有一个gridview1,它是父网格视图,我想在父网格视图的每一行内插入另一个gridview2,即子网格视图

这是.aspx

中的代码
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:GridView ID="gridView2" runat="server">
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是我在RowDataBound事件中添加的代码,我只是将gridview2与arraylist绑定,arraylist是根据每行发票号的内容过滤数据

 protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (GridViewRow gridviewrow in GridView1.Rows)
            {
                gridView2.AutoGenerateColumns = true;
                String x = gridviewrow.Cells[1].Text;
                softwareTitlesList = SoftwareListRetrieve();
                ArrayList titles = new ArrayList();
                foreach (SoftwareTitles softwareTitle in softwareTitlesList)
                {
                    if (softwareTitle.InvoiceNumber.Contains(x))
                        titles.Add(softwareTitle.SoftwareTitle);
                }
                gridView2.DataSource = titles;
                gridView2.DataBind();
            }
        }
    }

但似乎没有发生任何事情。

请帮帮我

感谢您的期待

2 个答案:

答案 0 :(得分:1)

一个问题是您在RowDataBound事件中执行此操作。对于绑定到数据源的GridView1中的每一行都将触发此操作。实质上,您每次都会重置GridView2的数据源。请尝试使用gridview的DataBound事件。

答案 1 :(得分:0)

当行绑定数据时,

RowDataBound事件触发。你必须这样做......

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;

            gridView2.AutoGenerateColumns = true;
            String x = dr["yourColumnName"].ToString();
            softwareTitlesList = SoftwareListRetrieve();
            ArrayList titles = new ArrayList();
            foreach (SoftwareTitles softwareTitle in softwareTitlesList)
            {
                if (softwareTitle.InvoiceNumber.Contains(x))
                    titles.Add(softwareTitle.SoftwareTitle);
            }
            GridView gridView2 = (GridView)e.Row.Findcontrol("gridView2");//add this
            gridView2.DataSource = titles;
            gridView2.DataBind();

    }
}

修改评论

GridView gridView2 = (GridView)e.Row.Findcontrol("gridView2");// add this line