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

Manual CRUD with Entity Reference

1 Answer 51 Views
Grid
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
Greetings,
I am implementing a grid using the manual crud example shown in your demo at: http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/programaticlinqupdates/defaultcs.aspx.  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" 
                        OnNeedDataSource="RadGrid1_NeedDataSource" 
                        OnColumnCreated="RadGrid1_ColumnCreated"
                        OnItemCreated="RadGrid1_ItemCreated" 
                        OnItemDataBound="RadGrid1_ItemDataBound"
                        OnInsertCommand="RadGrid1_InsertCommand"
                        OnUpdateCommand="RadGrid1_UpdateCommand"
                        OnDeleteCommand="RadGrid1_DeleteCommand" 
                        AllowFilteringByColumn="True" 
                        GridLines="None"    >
<HeaderContextMenu EnableAutoScroll="True"></HeaderContextMenu>
  
                <MasterTableView HierarchyDefaultExpanded="false" 
                                        HierarchyLoadMode="ServerBind"
                                        AllowSorting="true" 
                                        AutoGenerateColumns="false" 
                                        CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage"
                                        DataKeyNames="SICItemID" >
                 <Columns>  
                        <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="">
                    </telerik:GridDropDownColumn>
</Columns>  
  
<EditFormSettings>
<EditColumn UniqueName="EditCommandColumn1"></EditColumn>
</EditFormSettings>
            </MasterTableView>
            <ClientSettings AllowExpandCollapse="true" EnableRowHoverStyle="true" />
        </telerik:RadGrid>
         <cc1:DomainDataSource runat="server" ID="ddsGetSICCodes" 
        DomainServiceTypeName="ID.DomainServices.ListTypesDomainService" 
        SelectMethod="GetSicCodes" EnableUpdate="true" EnableInsert="false" EnableDelete="false" >
            <SelectParameters>
                <asp:SessionParameter Name="Scope" SessionField="MyScope" DefaultValue="" ConvertEmptyStringToNull="true" DbType="Guid" />
            </SelectParameters>
        </cc1:DomainDataSource>

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;
          try
          {
              ListTypesDomainService lds = new ListTypesDomainService();
              Guid sicItemID = new Guid(editedItem.GetDataKeyValue("SICItemID").ToString());
              var updateSicItem = lds.GetSicItem(sicItemID);
              if (updateSicItem != null)
              {
                  editedItem.UpdateValues(updateSicItem);
                  lds.SaveChanges();
              }
          }
          catch (Exception ex)
          {
              ProcessException(RadGrid1, ex);
              e.Canceled = true;
          }
      }

Thanks!

1 Answer, 1 is accepted

Sort by
0
Accepted
Martin
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

Greetings,
Martin
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
Tags
Grid
Asked by
Fred Dennis
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or