问题
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?
回答1:
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;
回答2:
Make sure you have load all the rows before you bind your data source with to binding list.
context.chaps.load
'load your data
gridcontrol1.datasource = context.chaps.Local.ToBindingList()
' this will load records on the grid. also give you a new row to add new or you are allowed to update the grid too.
来源:https://stackoverflow.com/questions/16721149/data-binding-directly-to-a-store-query-dbset-dbquery-dbsqlquery-is-not-suppo