How to extract primary key from parent radgrid to Nested radgrid

4 posts, 0 answers
  1. Patrick
    Patrick avatar
    11 posts
    Member since:
    Mar 2009

    Posted 21 Sep 2009 Link to this post

    Hello forum,

    I have a radgrid (radgrid1) nested in another (radgrid2).  The primary key for radgrid1 is "CustomerID" as is the foreign key in radgrid2.

    How do I get the selected primary value in radgrid1 to automatically appear during inserts in radgrid2, as the FKey, so that the user doesn't have to manually enter it? 

    Thanks.

  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 24 Sep 2009 Link to this post

    Hi Patrick,

    In order to obtain the primary key value of the parent item, attach an event handler to the ItemCommand event of the grid and use the following code:

    string CustomerID = e.Item.OwnerTableView.ParentItem.GetDataKeyValue("CustomerID").ToString(); 

    I hope this helps.

    Best Regards,
    Tsvetoslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Iman
    Iman avatar
    4 posts
    Member since:
    May 2012

    Posted 07 Jun 2012 Link to this post

    hi Tsvetoslav

    i have same problem but
    i have a nested items in Nested View template mode and i have a Command button on the Nested view Panel like this:
    <NestedViewTemplate>
          <asp:Panel ID="NestedViewPanel" runat="server" CssClass="viewWrap">
              <div class="contactWrap">
                  <fieldset style="padding: 10px;">
                      <legend style="padding: 5px;"><b>Detail info for Customer:<%# Eval("OrderID")%></b></legend>
                      <asp:Label ID="cityLabel" Text='<%#Bind("StuffID") %>' runat="server"></asp:Label>Test
                      <asp:Button ID="Button1" Text="Test" runat="server" CommandName="testcommand"/>
                  </fieldset>
              </div>
          </asp:Panel>
    </NestedViewTemplate>

    when I click my button in nested area i cannot get the primary key or other column value of Master Table in code behind. and get error message "Object reference not set to an instance of an object."
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "RowClick")
        {
            RadGrid parentGrid = (RadGrid)sender;
            GridDataItem gdi = parentGrid.MasterTableView.Items[e.Item.ItemIndex];
            gdi.Expanded = ! gdi.Expanded;
        }
        if (e.CommandName == "testcommand")
        {
            string entityid = e.Item.OwnerTableView.ParentItem.GetDataKeyValue("orderID").ToString();
        }
    }


    help me please

    regards.
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Jun 2012 Link to this post

    Hello Iman,

    Try the following code to access the DataKeyNames.
    C#:
    protected void grid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
     if (e.CommandName == "testcommand")
     {
      GridDataItem item = ((GridNestedViewItem)e.Item).ParentItem as GridDataItem;
      string ID = item.GetDataKeyValue("orderID").ToString();
     }
    }

    Thanks,
    Shinu.
Back to Top