Sreedhar Danturthi
Sreedhar Danturthi

Reputation: 7571

problem with the nested gridview asp.net/C#

I have a gridview1 which is the parent gridview and I want to insert another gridview2 which is child gridview inside every row of parent gridview

This is the code in the .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>

This is the code that I have added inside the RowDataBound Event and i'm just binding the gridview2 with the arraylist which is filtered data depending upon the contents of each row's invoice number

 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();
            }
        }
    }

But nothing seems to be happening.

Please help me

Thanks in anticipation

Upvotes: 0

Views: 3014

Answers (2)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

RowDataBound event fire when Rows bind the data. you have to do like...

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();

    }
}

Edit for comments:

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

Upvotes: 0

jmccarthy
jmccarthy

Reputation: 480

One problem is that you are doing this inside the RowDataBound Event. This is going to be fired for every row in GridView1 that is bound to the datasource. You are essentially resetting the DataSource for GridView2 every time. Try using the DataBound Event of the gridview instead.

Upvotes: 1

Related Questions