Data binding directly to the storage request (DbSet, DbQuery, DbSqlQuery) is not supported by Entity Framework 5

I have a devexpress GridControl for which I set it as a data source:

var regs = (from vcap in context.chaps select vcap); gridControl1.DataSource = new BindingList<chaps>(regs.ToList()); 

But when I use the grid, the rows that I add or delete are not saved, only the changes to the original rows are saved.

If I do this:

 gridControl1.DataSource = context.chaps.Local; 

I do not receive any lines, and AddNewRow does not even add a new line visually.

If I do this:

 gridControl1.DataSource = context.chaps.ToList(); 

I get strings and can save changes to them; lines look visually, but not in db, and cannot AddNewRow .

If I do this:

 gridControl1.DataSource = context.chaps; 

I get this exception:

 Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList(). 

but context.chaps.Local also does not have a ToBindingList method.

I don’t think this is a devexpress problem, but I don’t understand how to set the data source correctly. Is there a way to get the equivalent of context.chaps.Local.ToBindingList() ?

+6
source share
2 answers

context.chaps.Local is an ObservableCollection<T> . But ToBindingList not an ObservableCollection<T> method, but an extension method in DbExtensions :

 public static BindingList<T> ToBindingList<T>( this ObservableCollection<T> source) where T : class; 

To use this method and see it with Intellisense, you need to include the appropriate namespace in the code file where you are trying to call ToBindingList() :

 using System.Data.Entity; 
+7
source

Make sure you load all the lines before binding the data source to the list of bindings.

context.chaps.load 'upload your data gridcontrol1.datasource = context.chaps.Local.ToBindingList() ', this will load the records into the grid. also give a new line to add a new one, or you are also allowed to update the grid.

+1
source

Source: https://habr.com/ru/post/945712/


All Articles