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

How to extract primary key from parent radgrid to Nested radgrid

3 Answers 144 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Patrick
Top achievements
Rank 1
Patrick asked on 21 Sep 2009, 04:40 PM
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.

3 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 24 Sep 2009, 08:47 AM
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.
0
Iman
Top achievements
Rank 1
answered on 07 Jun 2012, 05:30 PM
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.
0
Shinu
Top achievements
Rank 2
answered on 08 Jun 2012, 05:01 AM
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.
Tags
General Discussions
Asked by
Patrick
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Iman
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or