title: Link button inside nested gridview invoking the wrong row
body: <p>I have a expand collapse nested grid view like below.</p>
  
  <p><img src="https://i.stack.imgur.com/dcC4b.gif" alt="enter image description here"></p>
  
  <p>The childgrid (gvChild) column is a linkbutton column on the click of which I need to get the name of the city which was clicked. My rowcommand event to capture that on the child gridview is like this:</p>
  
  <pre><code>protected void gvChild_RowCommand(object sender, GridViewCommandEventArgs e)
  {
        if (e.CommandName == "ShowDetails")
              {
                  int index = Convert.ToInt32(e.CommandArgument);
  
                  GridView gridChild = (GridView)gvwParent.Rows[index].FindControl("gvChild");
  
                  foreach (GridViewRow row in gridChild .Rows)
                  {
                    string aCity= ((LinkButton)row.Cells[1].FindControl("lnkSelectedCity")).Text;
                  }
          }
  }  
  </code></pre>
  
  <p>I understand the findcontrol line is def wrong, but how else would I do the findcontrol of the child grid to get the right link button that was clicked? Right now if I select "London", the Index value is 1 and the String "aCity" is taking it as "Utah". Will appreciate inputs on this. Thank you.</p>
  
  <p>Code on my aspx page:</p>
  
  <pre><code>&lt;asp:GridView ID="gvParent" runat="server" 
   AllowPaging="True" AutoGenerateColumns="False" 
     DataKeyNames="CountryID" 
         OnRowDataBound="gvParent_RowDataBound" &gt;     
             &lt;Columns&gt;     
               &lt;asp:TemplateField&gt;        
                &lt;ItemTemplate&gt;        
                   &lt;a href="javaScript:divexpandcollapse('div&lt;%# Eval("CountryID") %&gt;');"&gt;
                    &lt;img id='imgdiv&lt;%# Eval("CountryID") %&gt;' width="9px" border="0" src="expand_white.gif"
                                   alt="" /&gt;&lt;/a&gt;       
                &lt;/ItemTemplate&gt;    
               &lt;/asp:TemplateField&gt;    
              &lt;asp:TemplateField HeaderText="CountryDesc"&gt;
             &lt;ItemTemplate&gt;
  &lt;asp:Label ID="lblEmpID" runat="server" Text='&lt;%#DataBinder.Eval(Container.DataItem, "CountryDesc") %&gt;'&gt;&lt;/asp:Label&gt;
               &lt;/ItemTemplate&gt;
                &lt;/asp:TemplateField&gt;   
                  &lt;asp:TemplateField&gt;     
                     &lt;ItemTemplate&gt; 
                        &lt;tr&gt;
                          &lt;td colspan="100%"&gt;
                           &lt;div id='div&lt;%# Eval("CountryID") %&gt;' style="display: none; position: relative; left: 15px; overflow: auto"&gt;                                  
         &lt;asp:GridView ID="gvChild" runat="server" AutoGenerateColumns="False" PageSize="5"
                Width="100%" Height="270px" ShowHeader="False"   OnRowDataBound="gvChild_RowDataBound"&gt;
                      &lt;Columns&gt;
                          &lt;asp:BoundField DataField="CityID" HeaderText="CityID"&gt;
                              &lt;HeaderStyle CssClass="Invisible" /&gt;
                              &lt;ItemStyle CssClass="Invisible" HorizontalAlign="Left" /&gt;
                          &lt;/asp:BoundField&gt;
                          &lt;asp:TemplateField&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:LinkButton ID="lnkSelectedCity" runat="server" Text='&lt;% # Eval("CityName") %&gt;'
                                      CommandName="ShowDetails" CommandArgument='&lt;%# Container.DataItemIndex %&gt;' ToolTip="Select x"&gt;&lt;/asp:LinkButton&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:BoundField DataField="Assigned" HeaderText="Assigned"&gt;
                              &lt;HeaderStyle CssClass="Invisible" /&gt;
                              &lt;ItemStyle CssClass="Invisible" HorizontalAlign="Left" /&gt;
                          &lt;/asp:BoundField&gt;
                          &lt;asp:BoundField DataField="CountryID" HeaderText="Assigned"&gt;
                              &lt;HeaderStyle CssClass="Invisible" /&gt;
                              &lt;ItemStyle CssClass="Invisible" HorizontalAlign="Left" /&gt;
                          &lt;/asp:BoundField&gt;
                            &lt;asp:BoundField DataField="ClassID" HeaderText="ClassID"&gt;
                              &lt;HeaderStyle CssClass="Invisible" /&gt;
                              &lt;ItemStyle CssClass="Invisible" HorizontalAlign="Left" /&gt;
                          &lt;/asp:BoundField&gt;
                           &lt;asp:BoundField DataField="UOM" HeaderText="UOM"&gt;
                              &lt;HeaderStyle CssClass="Invisible" /&gt;
                              &lt;ItemStyle CssClass="Invisible" HorizontalAlign="Left" /&gt;
                          &lt;/asp:BoundField&gt;
                          &lt;/Columns&gt;
                  &lt;/asp:GridView&gt;
                  &lt;/div&gt;     
                 &lt;/td&gt;&lt;/tr&gt;
                &lt;/ItemTemplate&gt;     
               &lt;/asp:TemplateField&gt;     
               &lt;/Columns&gt;    
              &lt;/asp:GridView&gt;
  </code></pre>