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. Image for the cookie policy date
close icon

SfDatagrid - Entity Framework "adding row" issue

Hi,

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


2 Replies

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

Addendum:

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?



JG Jai Ganesh S Syncfusion Team 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()) 
            { 
                entity.Categories.Add(category); 
                entity.SaveChanges(); 
            }
        }
 
 
 
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"  
                        AddNewRowPosition="Top"  
                        AllowEditing="True"  
                        AllowFiltering="True"  
                        AllowGrouping="True"  
                        AllowSorting="True"  
                        AutoGenerateColumns="True"  
                        SourceType="{x:Type local:Category}"  
                        ItemsSource="{Binding Orders}" />  
 
 
Regards, 
Jai Ganesh S 


Loader.
Live Chat Icon For mobile
Up arrow icon