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

Inserts in Detail Table

1 Answer 41 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 09 Sep 2012, 08:40 PM
I have a detail table with a ParentTableRelation setup. The child rows are displaying and filtering for the parent record, I want to be able to add new rows where the newly inserted row includes the ID of the parent into it's forgein key. I had thought that setting up a ParentTableRelation might automatically do this, but saving fails due to the forgein key not being supplied.

Are there any examples of this?

If there's no simple way to do this I could always set a hidden field when the row is created. 

1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 13 Sep 2012, 08:03 AM
Hello Daniel,

You can use the InsertCommand event to achieve you goal:

protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
    // Detect the Detail Table name
    if ("Orders".Equals(e.Item.OwnerTableView.Name))
    {
        // Get the Parent Item in which the Detail Table is placed
        GridDataItem parentItem = (GridDataItem)e.Item.OwnerTableView.ParentItem;
        // Set the DataKeyValue of the parent item as a parameter of the DataSource control
        SqlDataSource2.InsertParameters["CustomerID"].DefaultValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["CustomerID"].ToString();
    }
}

More information on the matter and a sample runnable demo is available here.

I hope this helps.

Regards,
Martin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or