Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Sir/madam got a huge problem here as you can see ive created a nested gridview. My problem is on the child gridview where in its column an imagebutton; If I click it I want to catch the data keys of the parent and child gridview. Is possible sir?
                        <asp:GridView ID="grdCategory" runat="server" AutoGenerateColumns="False"   
                            Width="900px" DataKeyNames="CourseCatID" Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt" CellPadding="4" ForeColor="#333333" GridLines="None">
                            <columns>
                             <asp:BoundField  HeaderText = "CourseCatID" DataField="CourseCatID" />
                             
                             <asp:TemplateField HeaderText="Course Category">
                                 <itemtemplate>
                                 
                                 
                                <a href="java<!-- no -->script:toggleDiv('mydiv<%# Eval(" coursecatid=")%>')"><asp:Image ID="img" onclick="javascript:Toggle(this);" runat="server" ImageUrl="~/Images/plus.gif"
                                ToolTip="Expand" Width="7px" Height="7px" ImageAlign="AbsMiddle"/></a>
                                
                                       <asp:Label ID="lbllastname" Height="15px" runat="server" Text='<%# Eval("CourseCatName")%>'> 
           
                               
                                                                        

                                      <div id="mydiv<%# Eval(" coursecatid=")%>" style="display:none">
                                                        
                                       <br />
                               
                                     
                             
                                            

                     <asp:ImageButton ID="ImageAdd" Height="17px" ImageUrl="Images/addCourse.png" runat="server" CommandName="cmdAdd" CommandArgument='<%# Eval("CourseCatID") %>' OnClick="ImageAdd_click"
                                        />
                                       <br />
                                       <br />
                               <asp:GridView ID="grdCourse" runat="server"  Width="800px" HorizontalAlign="Right" AutoGenerateColumns="False" align="center" DataKeyNames="CourseName" Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt" BackColor="White" 
                              GridLines="Vertical">
<columns> <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left"  >
                                 <itemtemplate>
                                     <asp:ImageButton ID="ImageDelete"  Height="15px" ImageUrl="Images/Delete.png" runat="server" CommandName="cmdDelete" CommandArgument='<%# Eval("CourseName") %>'
                                        />
                                 </itemtemplate>
                                   <itemstyle horizontalalign="Left" />
                                </columns>
                               
</div></itemtemplate></columns>


Here is my code on RowCommand
Protected Sub grdCategory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCategory.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim gv As GridView = DirectCast(e.Row.FindControl("grdCourse"), GridView)
            _courselistDAL = New CourseListDAL
            Dim tpTemplate As New TrainingPlanTemplate

            tpTemplate.Year = 2011
            tpTemplate.CourseCatID = DataBinder.Eval(e.Row.DataItem, "CourseCatID")

            gv.DataSource = _courselistDAL.GetCourseDefinition(tpTemplate)
            gv.DataBind()
            grdCategory.Columns(0).Visible = False
            grdCategory.Columns(3).Visible = False
            'gv.Columns(0).Visible = False
            gv.Columns(5).Visible = False

        End If
Posted
Updated 4-Dec-11 20:11pm
v2

why not you put an other label just above you child grid and bind with you datakeys(primery key) and on row command you can call
C#
int ParentKey =  Convert.ToInt32(((Label)gvChildGrid.Parent.FindControl("lblPrimeryKey")).text)


this will give the datakeys of that perticular row of parent grid.
and for childgrid you can eaisly find on row command as commandargument.
 
Share this answer
 
On row command event, you can access parent grid from child grid like this..

C#
Protected void grdCategory_RowCommand(Object sender, GridViewRowEventArgs e)
{
   if(e.CommandName=="cmdDelete")
   {
       GridView ChildGridView = GridView child = (GridView)((ImageButton)(e.CommandSource)).Parent.Parent.Parent.Parent;
       GridView ParentGridView = (GridView)(ChildGridView.Parent.Parent.Parent.Parent);
   }
}
 
Share this answer
 
Comments
janwel 5-Dec-11 2:09am    
Sir I have a code on my rowcommand of grdCategory.. I cant read your code sir cause im on vb.net. Please see my Update code for my rowcommand

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900