使用 Winforms 和 EF 4.1 进行数据绑定 代码优先

发布于 2024-11-20 00:12:51 字数 782 浏览 11 评论 0原文

我试图让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬心 2024-11-27 00:12:51

每次添加行时,您都需要触发事件并调用绑定。

You need to fire an event and call bind each time a row is added.

因为看清所以看轻 2024-11-27 00:12:51

Mark W 引导我找到了正确的方法:

//put an event handler on the collection
public void CollectionChanged<T>(System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler) where T : class
{
     LocalDbContext.Set<T>().Local.CollectionChanged += eventHandler;
}

在表单类 (_sweepCollection) 中使用私有 BindingList

在第一个数据绑定上,设置事件处理程序:

private void BindSweepList()
{            
    _sweepCollection = _dataAccess.TopSweeps();
    _dataAccess.CollectionChanged<CableSweepDebug>(new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Local_CollectionChanged));

    UpdateSweepsData();
}

private void Local_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
     UpdateSweepsData();
}

private void UpdateSweepsData()
{
    if (comboBoxSweepIds.InvokeRequired)
    {
        Invoke(new UpdateSweepCount(UpdateSweepsData));
    }
    else
    {
        var tmpBind = _sweepCollection.OrderByDescending(t => t.BaseSweepId).Take(100);

        comboBoxSweepIds.DataSource = null;
        comboBoxSweepIds.DataSource = tmpBind.ToList() ;
        comboBoxSweepIds.DisplayMember = "BaseSweepId";
        comboBoxSweepIds.ValueMember = "BaseSweepId";                

    }
}

Mark W led me the right way:

//put an event handler on the collection
public void CollectionChanged<T>(System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler) where T : class
{
     LocalDbContext.Set<T>().Local.CollectionChanged += eventHandler;
}

Use a private BindingList<T> in the form class (_sweepCollection)

On the first databind, set up the event handler:

private void BindSweepList()
{            
    _sweepCollection = _dataAccess.TopSweeps();
    _dataAccess.CollectionChanged<CableSweepDebug>(new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Local_CollectionChanged));

    UpdateSweepsData();
}

private void Local_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
     UpdateSweepsData();
}

private void UpdateSweepsData()
{
    if (comboBoxSweepIds.InvokeRequired)
    {
        Invoke(new UpdateSweepCount(UpdateSweepsData));
    }
    else
    {
        var tmpBind = _sweepCollection.OrderByDescending(t => t.BaseSweepId).Take(100);

        comboBoxSweepIds.DataSource = null;
        comboBoxSweepIds.DataSource = tmpBind.ToList() ;
        comboBoxSweepIds.DisplayMember = "BaseSweepId";
        comboBoxSweepIds.ValueMember = "BaseSweepId";                

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文