0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

can u put your idea into coding. It would be more clear for me. Please !! Thanks in anticipation !
But if you put it inside a DataBound, then how are you going to iterate through each row updating GridView2 and making it refresh for each row? I tried this and it made all the GridView2 data look the same as the first row's GridView2.
0

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 

4 Comments

I have a new problem now it is giving me error "The name 'gridView2' does not exist in the current context" Is there any thing else i need to add to be able to access inside the .aspx.cs file
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) Even this method is not getting called !! What is the problem ?
you have to add event in your Gridview <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
Thanks Muhammad !! I owe you. Thank you for immediate responses. It worked. Have a nice day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.