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

Getting the ParentGrid data key when using a NestedViewTemplate

1 Answer 127 Views
Grid
This is a migrated thread and some comments may be shown as answers.
marksweat
Top achievements
Rank 1
marksweat asked on 06 Jun 2017, 12:52 AM

I'm following along with the "Grid - Hierarchy with Templates" example. There's a parent table, when the parent table row is expanded a template with several tabs is displayed, each tab contains a sub grid of the parent.  For the most part my version of this example works great.

The example seems to use "a little magic" to communicate the data key for the parent rows to the controls in the NestedViewTemplate. An ASP Label's text is bound to the data key for the rows (it is set not visible). A reference to the magic Label is then used in the ObjectDataSource to fetch sub grid data as needed.

My issues relates to having to do some validation of data before new data is Inserted. To do the validation I need to know the parent table row's data key in the code behind from the magic Label. I know how to get the Label's for all rows such as in the code example below. My issue is figuring out which Label to take the key from as there's a Label for each row in the Parent table.

 

    protected void BudgetAdjustmentItemRadGrid_InsertCommand(object sender, GridCommandEventArgs e)
    {

        Label dataKeyLabel = null;
        int id = 0;
        foreach (GridNestedViewItem item1 in ParentRadGrid.MasterTableView.GetItems(GridItemType.NestedView))
        {
            dataKeyLabel = (Label)item1.FindControl("DataKeyLabel");
            if (dataKeyLabel != null)
            {
                id = Convert.ToInt32(dataKeyLabel.Text);
            }
        }

    }

 

Maybe there's a better way to get the parent table row's data key. Let me know if you know one. I did find something that looked promising in the documentation. There's reference to a Parent Data Key and Detail Data Key such as below. However I can't find a working example of these settings. Furthermore my app won't compile when I attempt to use them.

 

       <NestedViewSettings DataSourceID="SqlDataSource2"><telerik:ParentTableRelation MasterKeyField="CustomerID" DetailKeyField="CustomeriD"/></NestedViewSettings>

1 Answer, 1 is accepted

Sort by
0
marksweat
Top achievements
Rank 1
answered on 06 Jun 2017, 09:41 PM

Googling around some more I found an approach that works.

 

http://www.telerik.com/forums/nested-view-get-datakey-of-expanded-parent-grid-row

Tags
Grid
Asked by
marksweat
Top achievements
Rank 1
Answers by
marksweat
Top achievements
Rank 1
Share this question
or