使用 Winforms 和 EF 4.1 进行数据绑定 代码优先
我试图让 Winforms 组合框在新行写入数据库时自动刷新。
POCO EF 类:
public class BaseSweep
{
public int BaseSweepId { get; set; }
//stuff removed for clarity
}
我通过这样的 BindingList 绑定到数据:
public BindingList<BaseSweep> TopSweeps()
{
LocalDbContext.BaseSweep.Load();
return LocalDbContext.BaseSweep.Local.ToBindingList();
}
private void BindSweepList() //called in Form_Load
{
comboBoxSweepIds.DataSource = _dataAccess.TopSweeps();
comboBoxSweepIds.DisplayMember = "BaseSweepId";
comboBoxSweepIds.ValueMember = "BaseSweepId";
}
这对于初始绑定效果很好,显示表中的当前 Id。当新行添加到表中时,LocalDbContext.BaseSweep.Local
中的计数会按预期增加。但是,comboBoxSweepIds
永远不会更新。有什么想法我做错了吗?
I'm trying to get a Winforms combobox to automatically refresh when new rows are written to our database.
POCO EF class:
public class BaseSweep
{
public int BaseSweepId { get; set; }
//stuff removed for clarity
}
I'm binding to the data through a BindingList like this:
public BindingList<BaseSweep> TopSweeps()
{
LocalDbContext.BaseSweep.Load();
return LocalDbContext.BaseSweep.Local.ToBindingList();
}
private void BindSweepList() //called in Form_Load
{
comboBoxSweepIds.DataSource = _dataAccess.TopSweeps();
comboBoxSweepIds.DisplayMember = "BaseSweepId";
comboBoxSweepIds.ValueMember = "BaseSweepId";
}
This works fine for the initial binding, shows the current Ids in the table. As new rows are added to the table, the count in LocalDbContext.BaseSweep.Local
goes up as expected. However, comboBoxSweepIds
never updates. Any ideas what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次添加行时,您都需要触发事件并调用绑定。
You need to fire an event and call bind each time a row is added.
Mark W 引导我找到了正确的方法:
在表单类 (
_sweepCollection
) 中使用私有BindingList
在第一个数据绑定上,设置事件处理程序:
Mark W led me the right way:
Use a private
BindingList<T>
in the form class (_sweepCollection
)On the first databind, set up the event handler: