【问题标题】:WinForms Data Binding Entity Framework 6WinForms 数据绑定实体框架 6
【发布时间】:2014-08-28 09:32:57
【问题描述】:

我在 WinForm 应用程序上有一个 ComponentOne Grid 控件,我想绑定一个“LINQ 查询”而不是整个 DbSet。

如果我需要完整的 DbSet,答案在这里:Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported Entity Framework 5

gridControl1.DataSource = context.capitulo.Local.ToBindingList();
context.capitulo.Load();

但是,如果我想下订单和/或 where 条件,例如:

gridControl1.DataSource = context.capitulo.OrderBy(x => x.Id).Local.ToBindingList();
context.capitulo.Load();

因为LocalDbSet 的属性,而不是IQuerable 的属性。 我找到了很多 WPF 的解决方案,但没有找到 WinForms 的解决方案。

【问题讨论】:

    标签: .net winforms linq entity-framework


    【解决方案1】:
    var capituloList = new ObservableCollection<capitulo>(context.capitulo.Local.OrderBy(x => x.Id));
    
    gridControl1.DataSource = capituloList.ToBindingList();
    
    context.capitulo.Load();
    

    【讨论】:

    • 使用此解决方案时,当我在网格上添加或删除一行时,我会错过绑定。它仅适用于更新现有值。
    【解决方案2】:

    您需要将 LINQ 查询应用于可绑定集合并获取另一个可绑定集合。这是使用 LiveLinq 完成的,它是 ComponentOne 套装的一部分(与 C1DataSource 一起,在 ComponentOne WinForms 中有一个),所以您可能拥有它。只需使用 AsLive() 扩展方法:

    gridControl1.DataSource = context.capitulo.Local.AsLive().OrderBy(x => x.Id);

    要使 AsLive() 可用,您需要

    使用 C1.WPF.LiveLinq;

    (但这里的WPF并不代表你在使用WPF,只是一个名字,你还在WinForms中)

    【讨论】:

    • 它给了我这个错误:集合不是 EntityCollection 类型的。
    猜你喜欢
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 2015-09-28
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多