我如何获得在Windows窗体DataGridView以显示新的记录时必然要EntityCollection [英] How do I get a Windows Forms DataGridView to show new records when bound to EntityCollection
问题描述
尝试新的记录添加到EntityCollection在运行,并与新的信息的DataGridView的更新。
Trying to add new records to the EntityCollection at runtime and have the the DataGridView update with the new information.
我试图在DataGridView直接绑定到实体集合(即对象集),并通过绑定到同一个集合的BindingSource。
I have tried bind the datagridview directly to the entity collection (i.e. ObjectSet) and through a BindingSource that is bound to the same collection.
我已经试过DataGridView.Refresh(),DataGridView.EndEdit(),和BindSource.ResetBindings()除其他事项外,但似乎没有任何工作。
I have tried DataGridView.Refresh(), DataGridView.EndEdit(), and BindSource.ResetBindings() among other things, but nothing seems to work.
推荐答案
我希望这不是为时已晚=)我有一些作品在这里...
I hope it's not too late =) I have something that works here...
// Entity Data Model
private ManagerEntities context = new ManagerEntities();
// declare private member
private BindingList<Currency> lstCurrencies = null;
// on form load, load data and bind to DataGridView's DataSource
private void Form1_Load(object sender, EventArgs e) {
lstCurrencies = new BindingList<Currency>();
ObjectResult or = ((ObjectQuery)currencies).Execute(MergeOption.AppendOnly);
foreach (Currency c in or)
lstCurrencies.Add(c);
// dgMain is my DataGridView
dgMain.DataSource = lstCurrencies;
}
// this will save objects that have changed. You might want to add logic for newly created and deleted objects.
private void btnSave_Click(object sender, EventArgs e) {
context.SaveChanges();
}
这篇关于我如何获得在Windows窗体DataGridView以显示新的记录时必然要EntityCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!