Click here to Skip to main content
14,391,415 members
Rate this:
Please Sign up or sign in to 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">
                             <asp:BoundField  HeaderText = "CourseCatID" DataField="CourseCatID" />
                             <asp:TemplateField HeaderText="Course Category">
                                <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" 
<columns> <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left"  >
                                     <asp:ImageButton ID="ImageDelete"  Height="15px" ImageUrl="Images/Delete.png" runat="server" CommandName="cmdDelete" CommandArgument='<%# Eval("CourseName") %>'
                                   <itemstyle horizontalalign="Left" />

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)
            grdCategory.Columns(0).Visible = False
            grdCategory.Columns(3).Visible = False
            'gv.Columns(0).Visible = False
            gv.Columns(5).Visible = False

        End If
Updated 4-Dec-11 21:11pm
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

On row command event, you can access parent grid from child grid like this..

Protected void grdCategory_RowCommand(Object sender, GridViewRowEventArgs e)
       GridView ChildGridView = GridView child = (GridView)((ImageButton)(e.CommandSource)).Parent.Parent.Parent.Parent;
       GridView ParentGridView = (GridView)(ChildGridView.Parent.Parent.Parent.Parent);
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 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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100