使用DataContext和LINQ to SQL绑定ComboBox

时间:2009-11-30 01:05:32

标签: c# winforms linq-to-sql

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

使用O / RM设计器,我对我的数据库建模类似于以下(简化):

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

这非常适合将发票表绑定到列表并查看客户列表,但我还没有完全了解如何在显示ComboBox时将客户表绑定到Customer发票。

以下是设置绑定的代码:

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

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

当我在发票清单中选择不同的项目时,客户框似乎没有显示我所做的任何更改。任何指针如何纠正这个?有没有更好的方法来设置此绑定?

1 个答案:

答案 0 :(得分:1)

您是否考虑过使用BindingContext

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

相关问题