Nested View - Get DataKey of Expanded Parent Grid Row

9 posts, 1 answers
  1. Scott
    Scott avatar
    4 posts
    Member since:
    May 2009

    Posted 05 Jun 2009 Link to this post

    Hi,

    I've implemented a nested view template very similar to the example Nested View Template however I am implementing Edit and Insert of items into grids that reside in the template.  On updates I'll be fine but on inserts, what I need is the datakey of the expanded row of the grid in the MasterTable.

    Please see aspx below.  When the user issues an Insert from the UserControl of grdEndorsements (AddEditEndorsement.ascx), what I need in order to complete the insert is the DataKeyNames="EntityID" from the expanded row of grdEntities.  So the logic here is, the user expands the nested view under a given row in grdEntities, they see the grdEndorsements in the RadTabStrip/Multipage and they can insert a new item into grdEndorsements.

    I know I can get the datakey name for grdEntities when it expands but I want to get it when the user actually inserts.  It is possible that the user may expand more than one row in grdEntities and then go and insert into a row previously expanded.  I've thought of a few possible solutions but open to suggestions.

    What I've left out for brevity is the SQL data source of grdEndorsements.  However, I bring it up since the Label ID="LabelEntityID" is the value I need to complete the insert, I use it as the parameter for the Data Source that feeds grdEndorsements and it works just fine.  I'm just not sure how to get that value on insert.

    Thanks.

    <telerik:RadGrid ID="grdEntities" runat="server" Skin="Telerik" 
    GridLines="None" AllowPaging="True" AllowSorting="True" Width="99%" AutoGenerateColumns="False" 
    ShowStatusBar="True" Visible ="true" ShowFooter="True">  
    <ClientSettings> 
        <Scrolling AllowScroll="False" /> 
        <ClientEvents OnPopUpShowing="PopUpShowing" /> 
    </ClientSettings> 
    <AlternatingItemStyle HorizontalAlign="Left" VerticalAlign="Middle" /> 
    <MasterTableView GridLines="None" Width="100%" CommandItemDisplay="Top"   
       DataKeyNames="EntityID" NoMasterRecordsText="No Entities to Display." EditMode="PopUp" EditFormSettings-CaptionFormatString="Add or Edit Entity">  
    <RowIndicatorColumn> 
        <HeaderStyle Width="20px" /> 
    </RowIndicatorColumn> 
    <CommandItemSettings AddNewRecordText="Add New Entity" /> 
    <ExpandCollapseColumn> 
        <HeaderStyle Width="20px" /> 
    </ExpandCollapseColumn> 
    <Columns> 
        <telerik:GridEditCommandColumn> 
        </telerik:GridEditCommandColumn> 
        <telerik:GridBoundColumn UniqueName="EntityID" HeaderText="EntityID" DataField="EntityID" Visible="False" > 
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn UniqueName="EntityType" HeaderText="EntityType" DataField="EntityType" Visible="False" > 
        </telerik:GridBoundColumn> 
         <telerik:GridBoundColumn UniqueName="EntityName" HeaderText="Name" DataField="EntityName" Visible="True" > 
        </telerik:GridBoundColumn> 
     
    </Columns> 
    <EditFormSettings  UserControlName="~/UserControls/AddEditEntity.ascx" EditFormType="WebUserControl" 
    PopUpSettings-Modal="True" PopUpSettings-Width="550px" PopUpSettings-ZIndex="9900">  
    <EditColumn UniqueName="EditCommandColumn1">  
    </EditColumn> 
     
    <PopUpSettings Modal="True" ZIndex="9900" Width="550px"></PopUpSettings> 
    </EditFormSettings> 
    <NestedViewTemplate> 
     
    <asp:Label ID="LabelEntityID" Font-Bold="true" Font-Italic="true" Text='<%# Eval("EntityID") %>' Visible="false" runat="server" /> 
     
    <asp:Panel runat="server" ID="InnerContainer" CssClass="viewWrap">  

    <
    telerik:RadTabStrip runat="server" ID="TabStip1" MultiPageID="Multipage1" SelectedIndex="0" Skin="Vista">  
    <Tabs> 
    <telerik:RadTab runat="server" Text="Endorsements" PageViewID="PageView1">  
    </telerik:RadTab> 
    </Tabs> 
    </telerik:RadTabStrip> 
    <telerik:RadMultiPage runat="server" ID="Multipage1" SelectedIndex="0" RenderSelectedPageOnly="false">  
    <telerik:RadPageView runat="server" ID="PageView1">  

    <
    telerik:RadGrid ID="grdEndorsements" runat="server" Skin="Web20" 
    GridLines="None" AllowPaging="True" AllowSorting="True" Width="99%" AutoGenerateColumns="False" 
    ShowStatusBar="True" Visible ="true" ShowFooter="True" DataSourceID="SDSGetEndorsements">  
    <MasterTableView GridLines="None" Width="100%" CommandItemDisplay="Top" HierarchyLoadMode="ServerOnDemand"   
    DataKeyNames="EndorsementID" NoMasterRecordsText="No Endorsements to Display." PageSize="5" 
    EditMode="PopUp" EditFormSettings-CaptionFormatString="Add or Edit Endorsement">  
    <CommandItemSettings AddNewRecordText="Add New Endorsement" /> 
    <Columns> 
    <telerik:GridEditCommandColumn> 
    </telerik:GridEditCommandColumn> 
    <telerik:GridBoundColumn UniqueName="EndorsementID" HeaderText="EndorsementID" DataField="EndorsementID" Visible="False" > 
    </telerik:GridBoundColumn> 
    <telerik:GridBoundColumn UniqueName="EntityID" HeaderText="EntityID" DataField="EntityID" Visible="False" > 
    </telerik:GridBoundColumn> 
    <telerik:GridBoundColumn UniqueName="EndorsementName" HeaderText="Name" DataField="EndorsementName" Visible="True" > 
    </telerik:GridBoundColumn> 
    </Columns> 
    <EditFormSettings  UserControlName="~/UserControls/AddEditEndorsement.ascx" EditFormType="WebUserControl">  
    <EditColumn UniqueName="EditCommandColumnE1">  
    </EditColumn> 
    <PopUpSettings Modal="True" ZIndex="9900" Width="550px"></PopUpSettings> 
    </EditFormSettings> 
    <ExpandCollapseColumn> 
    <HeaderStyle Width="20px" /> 
    </ExpandCollapseColumn> 
    <RowIndicatorColumn> 
    <HeaderStyle Width="20px" /> 
    </RowIndicatorColumn> 
     
    </MasterTableView> 
     
    <ClientSettings> 
    <ClientEvents OnPopUpShowing="PopUpShowing" /> 
    <Scrolling AllowScroll="False" /> 
    </ClientSettings> 
    <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="True" /> 
    <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" /> 
    <AlternatingItemStyle HorizontalAlign="Left" VerticalAlign="Middle" /> 
    </telerik:RadGrid> 
     
    </telerik:RadPageView> 
     
    </telerik:RadMultiPage> 
    </asp:Panel> 
    </NestedViewTemplate> 
    </MasterTableView> 
    <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" /> 
    <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="True" />                 
    </telerik:RadGrid> 
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 Jun 2009 Link to this post

    Hello Scott,

    You can try out the following code to access the datakeyvalue of the parent row while inserting into a nested grid:
    c#:
    protected void grdEndorsements_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            if (e.CommandName == "PerformInsert"
            { 
                GridEditFormInsertItem insertitem = (GridEditFormInsertItem)e.Item; 
                GridTableView nestedview = (GridTableView)insertitem.OwnerTableView; 
                GridNestedViewItem nestedviewItem = (GridNestedViewItem)nestedview.NamingContainer.NamingContainer; 
                GridDataItem dataItem = (GridDataItem)nestedviewItem.ParentItem; 
                string entityid = dataItem.GetDataKeyValue("EntityID").ToString(); 
                // insert query
            } 
        } 

    Thanks
    Princy.
  3. Scott
    Scott avatar
    4 posts
    Member since:
    May 2009

    Posted 08 Jun 2009 Link to this post

    Princy,

    Thanks a lot.  That works perfectly.  I appreciate the help.

    - Scott
  4. John John
    John John avatar
    132 posts
    Member since:
    Aug 2009

    Posted 24 Feb 2010 Link to this post

    Hi Princy,
          thats what i am looking for..

    -Thanks

  5. Karl Wilkens
    Karl Wilkens avatar
    206 posts
    Member since:
    Oct 2012

    Posted 04 Mar 2010 Link to this post

    Hi, is there a way to do this in ItemCreated rather than Command? I need to get the datakeyvalues which are set for both the mastertable and the detail table, when clicking the Add New Record button - prior to doing an insert?

    I tried this                EventID = CInt(e.Item.OwnerTableView.DetailTables(0).DataKeyValues(e.Item.ItemIndex)("EventID"))

    but it does not work and throws an error.

     Thanks.
  6. Kiran
    Kiran  avatar
    57 posts
    Member since:
    Oct 2018

    Posted 30 Dec 2019 Link to this post

    Hi,

    I am looking for the opposite.

    Radgrid =ONHANDS

    Within ONHANDS - NestedViewTemplate with ONHANDS_NESTED. No asp:panel.

    Within ONHANDS_ItemCommand, I want to access dataKeyvalues ofONHANDS_NESTED.

     

     

     If e.CommandName = "HOTSHEET" Then
                e.Item.Selected = True
                Dim item As GridDataItem = Nothing
                Dim strPlant As String = rgOnHands.MasterTableView.DataKeyValues(e.Item.ItemIndex)("PLANT")
                Dim strSid As String
                Dim innerGrid As RadGrid = TryCast((TryCast(e.Item, GridDataItem)).ChildItem.FindControl("rgOnHands_Nested"), RadGrid)
                'Dim innerGrid As RadGrid = TryCast(item.ChildItem.FindControl("rgOnHands_Nested"), RadGrid)
                'Dim innerGrid As RadGrid = (TryCast((TryCast(rgOnHands.MasterTableView.Items(0).ChildItem, GridNestedViewItem)).FindControl("rgOnHands_Nested"), RadGrid))

                For Each dataItem As GridDataItem In innerGrid.Items
                    strSid = dataItem.GetDataKeyValue("SID")
                Next
                'If TypeOf e.Item Is GridNestedViewItem Then
                '    For Each Item As GridDataItem In innerGrid.Items
                '        Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
                '        strSid = dataItem.GetDataKeyValue("SID")
                '    Next
                'End If

                MsgBox(strSid)


            End If

     

     

     

    <telerik:RadGrid ID="rgOnHands" runat="server" MasterTableView-ShowHeader="true"  AutoGenerateColumns="False" DataSourceID="" EnableEmbeddedskins="TRUE" GridLines="None" GroupingEnabled="true" ShowFooter="True" ShowHeader="TRUE" Skin="Black">
                                        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Black">
                                        </HeaderContextMenu>
                                        <MasterTableView DataKeyNames="PLANT, TWEIGHT" DataMember="Parent" DataSourceID="" GroupLoadMode="Client" Name="ONHANDS">
                                            <EditFormSettings>
                                                <EditColumn CancelImageUrl="Cancel.gif" EditImageUrl="Edit.gif" InsertImageUrl="Update.gif" UpdateImageUrl="Update.gif">
                                                </EditColumn>
                                            </EditFormSettings>
                                            <NestedViewTemplate>
                                                <telerik:RadGrid ID="rgOnHands_Nested" runat="server" AutoGenerateColumns="False" EnableEmbeddedskins="TRUE" GridLines="None" GroupingEnabled="true" OnItemCommand="rgOnHands_Nested_ItemCommand" OnNeedDataSource="rgOnHands_Nested_NeedDataSource" OnSortCommand="rgOnHands_Nested_SortCommand" ShowFooter="True" ShowHeader="TRUE" Skin="Black">
                                                    <MasterTableView runat="server" AllowPaging="false" AllowSorting="true" AutoGenerateColumns="False" DataKeyNames="PLANT, DOCK,DUNS,SID,SHIP_DAY,ROUTE,TRAILER, WEIGHT" Name="CHILD" ShowGroupFooter="true" TableLayout="Auto" Width="100%">
                                                        <ParentTableRelation>
                                                            <telerik:GridRelationFields DetailKeyField="PLANT" MasterKeyField="PLANT" />
                                                        </ParentTableRelation> and so on.

  7. Eyup
    Admin
    Eyup avatar
    4075 posts

    Posted 02 Jan 2020 Link to this post

    Hello Kiran,

     

    You can use the following approach to achieve this requirement:

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "HOTSHEET")
            {
                GridDataItem item = e.Item as GridDataItem;
                // access the inner grid
                RadGrid innerGrid = item.ChildItem.FindControl("rgOnHands_Nested") as RadGrid;
    
                foreach (GridDataItem innerItem in innerGrid.Items)
                {
                    innerItem.BackColor = System.Drawing.Color.Lime;
                }
            }
        }
    I hope this will prove helpful. I've tested it on my side and it works as expected.

     

    Regards,
    Eyup
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  8. Kiran
    Kiran  avatar
    57 posts
    Member since:
    Oct 2018

    Posted 02 Jan 2020 in reply to Eyup Link to this post

    Hi Eyup,

    That does not seem to work in my case. Does not even go into the For each loop.

     

            If e.CommandName = "HOTSHEET" Then

                CheckSession()
                e.Item.Selected = True

                Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
                Dim innerGrid As RadGrid = TryCast(item.ChildItem.FindControl("rgOnHands_Nested"), RadGrid)
                Dim strsid As String
                For Each innerItem As GridDataItem In innerGrid.Items
                    strsid = innerItem.GetDataKeyValue("SID")
                Next

    end if

  9. Eyup
    Admin
    Eyup avatar
    4075 posts

    Posted 07 Jan 2020 Link to this post

    Hello Kiran,

     

    When binding the nested grid programmatically, add also its DataBinding event handler as demonstrated here:
    https://www.telerik.com/support/code-library/radgrid-nestedviewtemplate-hierarchy-with-programmatic-binding-and-accessing-controls

     

    Regards,
    Eyup
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top