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

[Solved] Master/Detail Grids: How to use the datakey from a selected master grid row as a pre-populated value in a detail grid

2 Answers 264 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jgill
Top achievements
Rank 1
jgill asked on 29 Dec 2009, 01:27 AM
Hello,
I have a scenario where there are two grids where one is a master grid and one is a detail grid, similar to http://www.telerik.com/help/aspnet-ajax/grdrelatedgrids.html (except with two grids instead of three).  When a user selects a row from the master grid then data related to that row/item displays in the detail grid.  In my scenario, users add data to the detail grid and relate it to the master by manually entering the foreign key value (see the attached image and example below).

Example: There is a master grid that is populated with rows of "people".  A user selects a particular person then the detail grid displays the items that belong to the particular person.  The selected person ID is 3.  Users can add or update items that belong to the person by entering data into the detail grid, however currently they have to look at the master grid to tell what the ID is of the person.  ID is one of the datakeys in the detail grid.  It is a foreign key to the master grid data.  When someone needs to insert data into the detail grid related to the item they have selected in the master grid, I would like to pre-populate the textbox value when the editor for a new item is displayed.


I would like to use the datakey/primary key from the master grid to populate a value of a field when users are INSERTing new items into the detail grid.

Is this possible and how?

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 29 Dec 2009, 06:59 AM
Hi,

Try out the following code to achieve the required:
c#:
 
protected void DetailGrid_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)  
        {  
            GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;  
            TextBox txtbx = (TextBox)insertItem["ID"].Controls[0]; // access the textbox in the detail grid  
            GridDataItem parentItem = (GridDataItem)MasterRadGrid.SelectedItems[0]; // access the selected item of the master grid  
            txtbx.Text = parentItem["ID"].Text;  // set the textbox value as the primmary key value of the selected row in the master grid  
        }  
    }  

Hope this helps..
Princy.
0
jgill
Top achievements
Rank 1
answered on 29 Dec 2009, 08:24 PM
Thanks that works!
Tags
Grid
Asked by
jgill
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
jgill
Top achievements
Rank 1
Share this question
or