使用DataContext和LINQ将ComboBox绑定到SQL [英] Binding a ComboBox using a DataContext and LINQ to SQL

查看:57
本文介绍了使用DataContext和LINQ将ComboBox绑定到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于跟踪发票的简单SQLite数据库.我最近决定为此编写一个应用程序,以此作为学习LINQ的借口.我发现了其他问题,但没有解决方案为我工作.

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

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

这非常适合将发票表绑定到列表并查看客户列表,但是我不太了解如何在显示发票的Customer时将客户表绑定到ComboBox.

以下是设置绑定的代码:

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.

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

解决方案

您是否使用过描述:)

I have a simple SQLite database I use to track invoices. I recently decided to write an application for it as an excuse to learn LINQ. I found other questions that address this issue, but none of the solutions worked for me.

Using the O/RM designer, I modeled my database similar to the following (simplified):

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

This works great for binding the invoices table to a list and seeing a list of customers, but I'm not quite getting how to bind the table of customers to a ComboBox while showing the Customer for the invoice.

Here's the code that sets up the binding:

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

In this case, _db is the DataContext generated by the O/RM.

As I select different items in the invoice list, the customer box doesn't seem to show any changes I've made. Any pointers how to correct this? Is there a better way to setup this binding?

解决方案

Have you looked at using a BindingContext?

And here's a slightly less opaque description :)

这篇关于使用DataContext和LINQ将ComboBox绑定到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆