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

Manual CRUD with Entity Reference

1 Answer 74 Views
This is a migrated thread and some comments may be shown as answers.
Fred Dennis
Top achievements
Rank 1
Fred Dennis asked on 03 Aug 2010, 04:54 PM
I am implementing a grid using the manual crud example shown in your demo at:  However, for my scenario I have an entity reference key for one of my columns for which I populate a GridDropDownColumn using a DomainDataSource object as shown in the code block below:

        <telerik:RadGrid ID="RadGrid1" runat="server" 
                        GridLines="None"    >
<HeaderContextMenu EnableAutoScroll="True"></HeaderContextMenu>
                <MasterTableView HierarchyDefaultExpanded="false" 
                                        CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage"
                                        DataKeyNames="SICItemID" >
                        <telerik:GridEditCommandColumn ButtonType="ImageButton" />
                        <telerik:GridBoundColumn DataField="SICItemID" HeaderText="ID" UniqueName="SICItemID"  SortExpression="SICItemID" Visible="false" ReadOnly="true" Display="false" />  
                        <telerik:GridBoundColumn  DataField="ItemName" HeaderText="Question" UniqueName="ItemName" ItemStyle-Wrap="true"  ItemStyle-Width="300"  SortExpression="ItemName" MaxLength="2000" ReadOnly="false" Display="true"   />    
                                             <telerik:GridDropDownColumn FooterText="SIC Code" UniqueName="ddlSICCodes" ReadOnly="false"
                        ListTextField="SICCode1" ListValueField="SICID" DataSourceID="ddsGetSICCodes"
                        HeaderText="SIC Code" DataField="SICCode.SICID" AllowSorting="true" DefaultInsertValue="">
<EditColumn UniqueName="EditCommandColumn1"></EditColumn>
            <ClientSettings AllowExpandCollapse="true" EnableRowHoverStyle="true" />
         <cc1:DomainDataSource runat="server" ID="ddsGetSICCodes" 
        SelectMethod="GetSicCodes" EnableUpdate="true" EnableInsert="false" EnableDelete="false" >
                <asp:SessionParameter Name="Scope" SessionField="MyScope" DefaultValue="" ConvertEmptyStringToNull="true" DbType="Guid" />

The problem that I am having is that my reference entity for the dropdown list is not been updated when I select a new value during the Update.  During Insert the reference is set correctly to the selected value using the ExtractValues method of the GridEditableItem. Please advise of changes I could make to ensure my reference is updated when using the UpdateValues of the GridEditableItem method as shown below.  I have already included the entity reference (SicCode) when I get the object for update.  I do not understand how the UpdateValues work behind the scense and would appreciate any insight you can share regarding the handling of foreign key columns.
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
          GridEditableItem editedItem = e.Item as GridEditableItem;
              ListTypesDomainService lds = new ListTypesDomainService();
              Guid sicItemID = new Guid(editedItem.GetDataKeyValue("SICItemID").ToString());
              var updateSicItem = lds.GetSicItem(sicItemID);
              if (updateSicItem != null)
          catch (Exception ex)
              ProcessException(RadGrid1, ex);
              e.Canceled = true;


1 Answer, 1 is accepted

Sort by
Telerik team
answered on 06 Aug 2010, 01:11 PM
Hello Fred Dennis,

Note that the UpdateValues method correctly retrieve all the values from the column editors. However you will need to set the values to the navigational properties explicitly since they are different entities. You have to fetch the relevant entity manually and set it to the navigational property before you save the changes to the context. I have attached a small sample that demonstrates the approach described above.

I hope this helps

the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Asked by
Fred Dennis
Top achievements
Rank 1
Answers by
Telerik team
Share this question