RadGrid for ASP.NET AJAX

RadControls Send comments on this topic.
Extracting primary key value for parent item in hierarchy on Update/Insert
Controls > RadGrid > Insert/Update/Delete records > Extracting primary key value for parent item in hierarchy on Update/Insert

Glossary Item Box

Sometimes it is helpful to know the parent item primary key value when updating/inserting child records in grid hierarchy - for example, to refresh the parent row on update action as well (when certain conditions are met) or to hide some edit field in the child insertion form for specific parent item. Below are the steps which can be followed to fetch the parent row primary field in these situations:

  • wire the ItemCommand event of the grid
  • in case e.CommandName is RadGrid.UpdateCommandName the e.Item.OwnerTableView.ParentItem instance will point to the item from the parent table to which the child records belong.
  • if e.CommandName is RadGrid.InitInsertCommandName or RadGrid.PerformInsertCommandName the case is identical - once again e.Item.OwnerTableView.ParentItem will reference the parent table row.

The code section shows how to get the primary key field for up-level rows using DataKeyNames/DataKeyValues:

ASPX/ASCX Copy Code
<telerik:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1" runat="server" Skin="Desert"
Width="95%" AutoGenerateColumns="False" PageSize="3" AllowSorting="True" AllowPaging="True"
>
<PagerStyle Mode="NumericPages"></PagerStyle>
<MasterTableView DataSourceID="AccessDataSource1" DataKeyNames="CustomerID" Width="100%">
 
<DetailTables>
  
<telerik:GridTableView DataKeyNames="OrderID" DataSourceID="AccessDataSource2" Width="100%"
   
runat="server" CommandItemDisplay="Top">
   
<ParentTableRelation>
    
<telerik:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" />
   
</ParentTableRelation>
   
<Columns>
    
<telerik:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" HeaderButtonType="TextButton"
     
DataField= "OrderID" UniqueName="OrderID">
    
</telerik:GridBoundColumn>
    
<telerik:GridBoundColumn SortExpression="OrderDate" HeaderText="Date Ordered" HeaderButtonType="TextButton"
     
DataField= "OrderDate" UniqueName="OrderDate">
    
</telerik:GridBoundColumn>
    
<telerik:GridBoundColumn SortExpression="EmployeeID" HeaderText="EmployeeID" HeaderButtonType="TextButton"
     
DataField= "EmployeeID" UniqueName="EmployeeID">
    
</telerik:GridBoundColumn>
    
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />
   
</Columns>
  
</telerik:GridTableView>
 
</DetailTables>
 
<Columns>
  
<telerik:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" HeaderButtonType="TextButton"
   
DataField="CustomerID" UniqueName="CustomerID">
  
</telerik:GridBoundColumn>
  
<telerik:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton"
   
DataField="ContactName" UniqueName="ContactName">
  
</telerik:GridBoundColumn>
  
<telerik:GridBoundColumn SortExpression="CompanyName" HeaderText="Company" HeaderButtonType="TextButton"
   
DataField="CompanyName" UniqueName="CompanyName">
  
</telerik:GridBoundColumn>
  
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />
 
</Columns>
</MasterTableView>
</
telerik:RadGrid>
<
asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Grid/Data/Access/Nwind.mdb"
SelectCommand="SELECT * FROM Customers" runat="server"></asp:AccessDataSource>
<
asp:AccessDataSource ID="AccessDataSource2" DataFile="~/Grid/Data/Access/Nwind.mdb"
SelectCommand="SELECT * FROM Orders Where CustomerID = ?" runat="server">
<SelectParameters>
 
<asp:Parameter Name="CustomerID" Type="string" />
</SelectParameters>
</
asp:AccessDataSource>

And in the code-behind:

VB.NET Copy Code
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
         If (e.CommandName = RadGrid.UpdateCommandName AndAlso e.Item.OwnerTableView.DataSourceID = "AccessDataSource2") Then
             Dim OrderID As String = e.Item.OwnerTableView.Items(e.Item.ItemIndex)( "OrderID").Text
            RadGrid1.Controls.Add( New LiteralControl("Updated item primary key value is: " & OrderID & "<br/>"))

             Dim parentItem As GridDataItem = e.Item.OwnerTableView.ParentItem

             If (Not parentItem Is Nothing) Then
                RadGrid1.Controls.Add( New LiteralControl("Parent item primary field value is: " &
                CType(parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)( "CustomerID"), String)))
              End If
         End If
         If (e.CommandName = RadGrid.InitInsertCommandName Or e.CommandName = RadGrid.PerformInsertCommandName) Then
             If (e.Item.OwnerTableView.DataSourceID = "AccessDataSource2") Then
                 Dim parentItem = CType(e.Item.OwnerTableView.ParentItem, GridDataItem)

                 If (Not parentItem Is Nothing) Then
                    RadGrid1.Controls.Add( New LiteralControl("Parent item primary field value is: " &
                    CType(parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)( "CustomerID"), String)))
                 End If
             End If
         End If
    End Sub
C# Copy Code
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
        
if (e.CommandName == RadGrid.UpdateCommandName && e.Item.OwnerTableView.DataSourceID == "AccessDataSource2")
       {
            
string OrderID = e.Item.OwnerTableView.Items[e.Item.ItemIndex][ "OrderID"].Text;

           RadGrid1.Controls.Add(
new LiteralControl("Updated item primary key value is: " + OrderID + "<br/>"));

            GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
            
if (parentItem != null)
           {
               RadGrid1.Controls.Add(
new LiteralControl("Parent item primary field value is: " +
               (
string)(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex][ "CustomerID"])));
           }
       }
        
if (e.CommandName == RadGrid.InitInsertCommandName || e.CommandName == RadGrid.PerformInsertCommandName)
       {
            
if (e.Item.OwnerTableView.DataSourceID == "AccessDataSource2")
           {
                GridDataItem parentItem = (GridDataItem)(e.Item.OwnerTableView.ParentItem);

                
if (parentItem != null)
               {
                   RadGrid1.Controls.Add(
new LiteralControl("Parent item primary field value is: " +
                   (
string)(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex][ "CustomerID"])));
               }
           }
       }
   }