代码之家  ›  专栏  ›  技术社区  ›  jheddings

使用DataContext和LINQ将组合框绑定到SQL

  •  0
  • jheddings  · 技术社区  · 16 年前

    我有一个简单的SQLite数据库,用于跟踪发票。我最近决定为它写一个应用程序,作为学习LINQ的借口。我发现 other questions 这解决了这个问题,但没有一个解决方案对我有效。

    使用O/RM设计器,我对数据库进行了如下建模(简化):

    +------------+           +----------+
    |  Invoice   |           | Customer |
    +------------+           +----------+
    | ID         |      +----| ID       |
    | CustomerID |<-----+    | Name     |
    +------------+           +----------+
    

    这对于将invoices表绑定到列表并查看客户列表非常有用,但我不太了解如何将客户表绑定到客户列表 ComboBox 在展示 Customer 为了发票。

    下面是设置绑定的代码:

    InvoiceList.DataSource = _db.Invoices;  // InvoiceList is ListBox
    CustomerBox.DataSource = _db.Customers;  // CustomerBox is ComboBox
    CustomerBox.DataBindings.Add("SelectedItem", InvoiceList.DataSource, "Customer");
    

    在这种情况下,, _db DataContext 由O/RM生成。

    当我在发票列表中选择不同的项目时,客户框似乎没有显示我所做的任何更改。有没有关于如何纠正这个问题的建议?是否有更好的方法设置此绑定?

    1 回复  |  直到 8 年前
        1
  •  1
  •   flesh    16 年前

    你有没有考虑过使用 BindingContext

    这是一个稍微不透明的 description :)

    推荐文章