We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

SfDatagrid - Entity Framework "adding row" issue

Thread ID:





125755 Sep 9,2016 09:28 AM UTC Sep 12,2016 12:47 PM UTC WPF 2
Tags: SfDataGrid
Jonas Züger
Asked On September 9, 2016 09:28 AM UTC


I've got a SfDataGrid which is data bound to an ObservableCollection of Entity Framework items.

When I add a new row and load the Entity Framework items again* an error message pops up:

Message=Das Objekt des Typs "DataLayer.EntitiesDB.SequenceEditorItem" kann nicht in Typ "System.Data.Entity.DynamicProxies.SequenceEditorItem_5090EDC7CFDC945E13DBDF0872CAB31598B97FE519E86F7BE4466D584C2402D6" umgewandelt werden.  HResult=-2147467262System.InvalidCastException was unhandled 

* Database.Hardware.Load();
hardwareViewSource.Source = Database.Hardware.Local;

Using the common MS DataGrid this does not happen. Furthermore Database.SaveChanges() does not solve the problem. What can be the reason for this?

I'm using VS2015, .NET4.6.1, EF6.1.3

Jonas Züger
Replied On September 9, 2016 12:09 PM UTC


After first investigation it seems that the SfDataGrid creates a "normal" object whereas the MS Datagrid creates an Entity Framework proxy item. Loading this back to the SfDataGrid causes an InvalidCastException.

Now my question is a more concrete: Is it possible to tell SfDataGrid to create a proxy item by the built in "add row" function?

Jai Ganesh S [Syncfusion]
Replied On September 12, 2016 12:47 PM UTC

Hi Züger, 
We have analyzed your query. You can achieve your requirement to add a new row in SfDataGrid when using EntityFrameworkCollection by wiring the AddNewRowInitiating event and save the changes like below, 
this.datagrid.AddNewRowInitiating += datagrid_AddNewRowInitiating;

void datagrid_AddNewRowInitiating(object sender, AddNewRowInitiatingEventArgs args)         
            Category newRecord = args.NewObject as Category; 
            Category category = new Category(); 
            category.Description = newRecord.Description; 
            using(Entities entity=new Entities()) 
UG Link: 
If you still facing the issue after trying the above way, then could you please set SourceType(Underlying Collection type) in your sample to resolve this issue like below, 
<syncfusion:SfDataGrid x:Name="dataGrid"  
                        SourceType="{x:Type local:Category}"  
                        ItemsSource="{Binding Orders}" />  
Jai Ganesh S 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon