I have a devexpress GridControl for which I am setting it's datasource like so:
var regs = (from vcap in context.chaps
select vcap);
gridControl1.DataSource = new BindingList<chaps>(regs.ToList());
But when I use the grid, rows I add or delete don't get saved, only changes to the initial rows get saved.
If I do this:
gridControl1.DataSource = context.chaps.Local;
I don't get any rows, and AddNewRow
doesn't even add a new row visually.
If I do this:
gridControl1.DataSource = context.chaps.ToList();
I get the rows and can save changes to them; rows get deteled visually but not in the db, and can't 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 does not have a ToBindingList
method either.
I don't think this is a problem of devexpress, but rather I'm not getting how to set a datasource properly. Is there a way to get a context.chaps.Local.ToBindingList()
equivalent?
context.chaps.Local
is an ObservableCollection<T>
. But ToBindingList
is not a method of ObservableCollection<T>
but an extension method in DbExtensions
:
public static BindingList<T> ToBindingList<T>(
this ObservableCollection<T> source) where T : class;
In order to use this method and see it with Intellisense you need to include the corresponding namespace in the code file where you try to call ToBindingList()
:
using System.Data.Entity;