This is a migrated thread and some comments may be shown as answers.

Accessing Parent DataKeyValues from Nested Grid Insert

1 Answer 162 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Megan
Top achievements
Rank 1
Megan asked on 05 Apr 2011, 02:35 PM
I have a Grid named gvModules with a nested grid called gvFields.  The gvModules uses the DataKey ItemTypeID that I need when a new record is added to gvFields.  I am trying to access that DataKey from the insert statement in the gvFields ItemCommand method.  I've tried the examples given on the forums and in the documentation without avail. I would welcome any additional direction.

ASPX
<telerik:RadGrid ID="gvModules" runat="server" AutoGenerateColumns="False" 
        DataSourceID="dsModules" GridLines="None" AutoGenerateEditColumn="True" 
        OnItemDataBound="gvModules_OnItemDataBoundHandler"
        Skin="Windows7" AllowFilteringByColumn="True" AllowSorting="True">
<MasterTableView DataKeyNames="ItemTypeID" DataSourceID="dsModules">
----
<NestedViewSettings DataSourceID="dsFields">
        <ParentTableRelation>
            <telerik:GridRelationFields DetailKeyField="ItemTypeID" MasterKeyField="ItemTypeID" />
        </ParentTableRelation>
    </NestedViewSettings>
    <NestedViewTemplate>
        <telerik:RadGrid ID="gvFields" runat="server" DataSourceID="dsFields" 
            AutoGenerateEditColumn="True" GridLines="None" Skin="Windows7" 
            OnItemDataBound="gvFields_OnItemDataBoundHandler"
            OnItemCommand="gvFields_ItemCommand" OnItemUpdated="gvFields_ItemUpdated" 
            AllowSorting="True">
            <MasterTableView CommandItemDisplay="TopAndBottom" AutoGenerateColumns="False" DataSourceID="dsFields" DataKeyNames="FieldID">
                -----
             </MasterTableView>
         </telerik:RadGrid>
      </NestedViewTemplate>
</MasterTableView>
</telerik:RadGrid>

C#
protected void gvFields_ItemCommand(object source, GridCommandEventArgs e)
{
        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
  
            //PARAM ITEM TYPE ID
            GridDataItem parentItem = (GridDataItem)(e.Item.OwnerTableView.ParentItem);
              
            if (parentItem != null)
            {
                string itemType = "";
                itemType = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["ItemTypeID"].ToString();
                lblTmp.Text += "Item Type ID = " + itemType;
                //dsFields.InsertParameters["ItemTypeID"].DefaultValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["ItemTypeID"].ToString();
            }
            else
            {
                lblError.Text += "Cannot find parent item";
            }
     }
}

What I always get is 'Cannot find parent item'. 

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 07 Apr 2011, 07:45 AM
Hello Megan,

You can try the following code snippet to achieve this.
C#:
protected void gvFields_ItemCommand(object sender, GridCommandEventArgs e)
   {
       if (e.CommandName == RadGrid.PerformInsertCommandName)
       {
           GridEditFormInsertItem insertItem = e.Item as GridEditFormInsertItem;
           RadGrid innergrid = (RadGrid)insertItem.OwnerTableView.OwnerGrid;
           GridNestedViewItem nestedItem = (GridNestedViewItem)innergrid.NamingContainer;
           GridDataItem parentitem = (GridDataItem)nestedItem.ParentItem;
           string itemType = parentitem.GetDataKeyValue("ItemTypeID").ToString();
       }
   }

Thanks,
Princy.
Tags
Grid
Asked by
Megan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or