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.
Unfortunately, activation email could not send to your email. Please try again.

SfDatagrid - Entity Framework "adding row" issue

Thread ID:

Created:

Updated:

Platform:

Replies:

125755 Sep 9,2016 05:28 AM Sep 12,2016 08:47 AM WPF 2
loading
Tags: SfDataGrid
Jonas Züger
Asked On September 9, 2016 05:28 AM

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


Jonas Züger
Replied On September 9, 2016 08:09 AM

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?


Jai Ganesh S [Syncfusion]
Replied On September 12, 2016 08:47 AM

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 


CONFIRMATION

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.

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.

;