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

Hierarchic grid: use parent key field as parameter of child objectdata source

2 Answers 159 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Damien
Top achievements
Rank 1
Damien asked on 17 Dec 2012, 04:54 PM
Hello,
I have a hierarchic grid. Something like that (I tried to keep only the interesting lines).
                        
<telerik:RadGrid ID="RadGrid1" runat="server">
 
               <MasterTableView AutoGenerateColumns="False"
                                     Name="LEVEL1"
                                     DataKeyNames="SourceID"
                                     CommandItemDisplay="Top"
                                     EditMode="EditForms"
                                     DataSourceID="ObjectDataSource1" >
 
                   <Columns>
                       <telerik:GridBoundColumn UniqueName="SourceID"
                                                       DataField="SourceID"
             ReadOnly="true" />
                   </Columns>
                    
                   <DetailTables>
                       <telerik:GridTableView AutoGenerateColumns="False"
                                                      Name="LEVEL2"
                                                      DataKeyNames="SourceID" 
                                                                                                            runat="server"
                                                      CommandItemDisplay="Top"
                                                      DataSourceID="ObjectDataSource4" >
 
                           <ParentTableRelation>
                               <telerik:GridRelationFields DetailKeyField="SourceID"
                                                                     MasterKeyField="SourceID" />
                           </ParentTableRelation>
 
                           <Columns>
                                   <telerik:GridBoundColumn UniqueName="SourceID"
                                                                       DataField="SourceID"
                                                                       ReadOnly="true" />
                                   <telerik:GridDropDownColumn UniqueName="ElementShortNameID"
                                                                          DataField="ElementID"
                                                                          HeaderText="Element Short Name ID"
                                                                          ReadOnly="false"
                                                                          DropDownControlType="RadComboBox"
                                                                          DataSourceID="ObjectDataSource5"
                                                                          ListTextField="ElementShortNameID"
                                                                          ListValueField="ElementID" />
                           </Columns>
 
                       </telerik:GridTableView>
                   </DetailTables>
 
                   </MasterTableView>
           </telerik:RadGrid>

At first level (MasterTableView), the records have a "data key name" (sourceID)
At second level, the records have also the same data key name (So I have a "grid relation fileds" between the two identifers)
At second level, there is another field (element) with a value provided by a radcombobox.
I want that, in edit mode, the available element values in the radcombobox depend on the sourceID.
See the following object data source:

<asp:ObjectDataSource ID="ObjectDataSource5" runat="server" TypeName="DataAccessLayer.DataSource.ElementDataSource" SelectMethod="GetElementsListBySource" >
                <SelectParameters>
                    <asp:Parameter Name="SourceID" Type="String"></asp:Parameter>
                </SelectParameters>
            </asp:ObjectDataSource>


It works correctly when I am in Edit mode in order to UPDATE a record. The sourceID used seems to be the one of the record.

But, when I enter in edit mode in order to INSERT a new record (via the "Add a record button"), the sourceID parameter, received by the objectdatasource method (GetElementsListBySource) is null. As the record still not exists, no sourceID can't be used.

My question is: How could a get the parent record sourceID to pass it to the objectdatasource method providing my elements ?
I would like, if possible, make that declaratively in the aspx file to keep the same usage of objectdatasource.

Thanks by advance.
Damien



2 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 20 Dec 2012, 01:33 PM
Hi Damien,

You can achieve your requirement by first attaching event handlers to the grid's ItemCommand event and the ObjectDataSource control's OnSelecting event. In the Item command event listen for the InitInsertCommand of a detail table and when caught raise a page-global flag. Then in the OnSelecting event of the data source check the flag and if true, set explicitly whatever value you need for the select parameter:
protected void ObjectDataSource5_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    if (initInsertRaisedForDetailTable)
    {     
         e.InputParameters["SourceID"] = ....//your value goes here
    }
}


Hope it helps.

Regards, Tsvetoslav
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.
0
Damien
Top achievements
Rank 1
answered on 21 Dec 2012, 09:01 AM
Hi Tsvetoslav,
Your solution works perfectly for me. Thank you very much.
Regards,
Damien
Tags
Grid
Asked by
Damien
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Damien
Top achievements
Rank 1
Share this question
or