0

I have an issue with nested Gridview!

I have two Girdviews with the ID - GridView1(Parent Gridview) and GV2(Child Gridview)

I have a Asp:Button with the ID - btnedit(Event = OnClick())

Qs: how to access GV2 in btnedit_Onclick event?

I have attached the Code for your Reference:

<form id="form1" runat="server">
        <asp:Button runat="server" ID="btnedit" Text="Edit" OnClick="btnedit_Click" />
        <asp:GridView runat="server" ID="Gridview1">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="Lable1"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:GridView runat="server" ID="GV2">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:Label runat="server" ID="Lable2"></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </form>

code Behind:

protected void btnedit_Click(object sender, EventArgs e)
  {

  }
    
Jeevan ebi
  • 105
  • 12
  • Well, then you click on a button outside of the GV, then what main GV row are you wanting to work with, and then what child row of the nested GV row are you looking to get and grab? Unless logic is setup to allow a user to select a row from the main GV, and then logic is setup to select a child GV row, then it hard to guess what row you looking to select/get here? – Albert D. Kallal Mar 02 '22 at 18:35
  • @TimSchmelter GV2 (Inner gridview) – Jeevan ebi Mar 03 '22 at 06:52
  • @AlbertD.Kallal Main GridView Row = 1, Child Gridview Row = 1. – Jeevan ebi Mar 03 '22 at 06:53
  • @AlbertD.Kallal Actually I am facing Problem with How to find Inner Gridview Control in a normal Button Click Event. I am able to Access GV2 GridView if I place the GV2 outside the parent Gridview, but in case of Nested Gridview I couldn't able to find the inner Gridview(Gv2) control from the Button click event ! (for Reference I have named edit click) – Jeevan ebi Mar 03 '22 at 06:57
  • Well, getting the nested GV is not a problem. the problem is what selected row are you going to operate on. Until you made clear what row you talking about, then we are 100% in the dark. so again: what row of the main grid are you to use, and how is the main row being selected? Once you have a given GV row in the main GV, then getting the child GV (and possible selected row) is trivial. But, how is a given row in teh main GV being selected? Or maybe you want to see/get all main GV rows, and for each child GV, you want to get the child GV - but again, getting everything don't make much sense. – Albert D. Kallal Mar 03 '22 at 16:48
  • if you saying that you have some row button or row click for the main GV row, and then you want to get the child GV nested? No probelm, but you are suggesting a button outside of both main GV, and the nested one - so then the question remains - what main row are you wanting to get, how do or did you get the main GV row. Once we have this answer, then we can post the answer to how you can get the child GV with great ease (and when you get the child GV, what row from the child GV do you want? (or maybe all of the child rows? - but we don't know, and thus we can't help you. – Albert D. Kallal Mar 03 '22 at 16:51
  • In other words, when you click the button outside of both GV main and child? What main GV row are we to use here? Do you have some kind of row selecting setup ? I mean, you click a button outside of main GV, then what row are we going to use here? Once you explain what row we are to use, and how you get that row, I'll post how to get the child GV. But without information as to what row the button outside is to get and operate from the main gv, then how can we start working on the problem to get the child row when we have no clue how and what main GV row we are to operate on. – Albert D. Kallal Mar 03 '22 at 16:56
  • See my solution posted below – Albert D. Kallal Mar 04 '22 at 03:49

2 Answers2

0

This may get you started...

GridView gvGV2 = e.Row.FindControl("GV2") as GridView;
gvGV2.DataSource = "..."; //Your DataSource
gvGV2.DataBind();
// or do something else.
Bill
  • 11
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 02 '22 at 15:38
  • @bill, thanks for your answer, but unfortunately it doesn't works!. Actually the "Row" is not recognized in normal Button Click event, it shows error! – Jeevan ebi Mar 03 '22 at 06:59
0

Ok, so the user wants to operate on the row 1 of main GV and then get row 1 of the child.

Gridviews start at 0, so it not clear, but lets take the both first rows.

next up? I really (but really really really) suggest you use a main listview and then nest a child GV.

If you nest two GV's, you tend to get this:

Say this GV, with a "+" to expand the child grid:

enter image description here

And expanding, you get this:

enter image description here

So, nesting grids is VERY hard, since the "col span" is hard to setup.

However, if I build a main listview (looks the same as a GV), and then next a gv, then I get this:

enter image description here

So above uses a main list view, and thus for expanding child reocrds I do and did use a GV in the child of the listview.

I can post how above works and even the markup if you wish.

However, lets go with the two nested GV's - it will make a mess of a UI but for learning, how this works with the above nested LV + GV, or a GV + GV is the same.

So, some plane jane button - outside of the two grids, get the first row of the main, then the first row of the child grid.

This works:

    protected void Button1_Click(object sender, EventArgs e)
    {
        GridViewRow MainGvRow = GridView1.Rows[0];
        GridView ChildGV = MainGvRow.FindControl("GridView2") as GridView;

        GridViewRow ChildGVRow = ChildGV.Rows[0]
    }
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51