实体框架4使用外键的WinForms组合框数据绑定 [英] Entity Framework 4 Databinding on WinForms ComboBoxes with Foreign Keys

查看:149
本文介绍了实体框架4使用外键的WinForms组合框数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Entity Framework 4,我需要获得WinForms绑定客户和报价作为一个主 - 细节关系。

I am using Entity Framework 4, I need to get WinForms to bind Customers and Quotes as a Master - Detail relationship.

我有一个报价表作为Windows窗体上的详细信息视图。

I have a Quote table which I lay out as details view on a Windows Form.

报价表具有3个外键到客户表。 (字段CustomerId,SiteCustomerId,InvoiceCustomerId,它们都链接到Customer表中的Id字段)。

The quote table has 3 foreign keys to the customer table. (fields CustomerId, SiteCustomerId, InvoiceCustomerId which all link to Id field in the Customer table).

在表单上有3个客户面板, ,以及textBox中的其他客户详细信息字段。

On the form there are 3 Customer panels with the Customer Name fields in a ComboBox, and other Customer detail fields in textBoxes.

如何连接组合框,以便它们显示客户表下拉列表中的所有可能的客户,正确的所选值,并保存到报价表中的正确CustomerId字段。

How do I wire up the combo boxes so that they display all the possible Customers in the drop down from the Customer table and have the correct Selected Value, and save to the correct CustomerId field in the Quote table.

我的(不良)尝试:

     Context = new Entities();
        quoteBindingSource.DataSource = Context.Quote;
    customersBindingSource.DataSource = Context.Customers;

        comboBox1.DataSource = customersBindingSource;
                    comboBox1.DisplayMember = "Customer";
                    comboBox1.ValueMember = "Id";

                    comboBox1.DataBindings.Clear();
                    comboBox1.DataBindings.Add("SelectedValue", quoteBindingSource, "CustomerId");

comboBox9.DataSource = customersBindingSource;
            comboBox9.DisplayMember = "Customer";
            comboBox9.ValueMember = "Id";

            comboBox9.DataBindings.Clear();
            comboBox9.DataBindings.Add("SelectedValue", quoteBindingSource, "InvoiceCustomerId");

            comboBox6.DataSource = customersBindingSource;
            comboBox6.DisplayMember = "Customer";
            comboBox6.ValueMember = "Id";

            comboBox6.DataBindings.Clear();
            comboBox6.DataBindings.Add("SelectedValue", quoteBindingSource, "SiteCustomerId");


推荐答案

谢谢你的WinForms项目版本:)发生,或者有些东西是错误的,或者我们可能发现了一种错误。在这样的情况下,通常假设我们缺乏某种知识更安全。解决方案很简单但奇怪:在每个Combobox中:

thank you for your WinForms project version:) Well it occurs, that either something is wrong or we may have discovered a kind of a bug. In situations like that it is usually safer to assume that we lack some kind of knowledge. The solution is simple yet weird: in every Combobox you do:

comboBox9.DataBindings.Add("SelectedValue", quoteBindingSource, "InvoiceCustomerId");

你应该做什么来改善情况,值回到旧的是:

What you should do to improve the situation with values coming back to old ones is:

comboBox9.DataBindings.Add(new Binding("SelectedValue", quoteBindingSource, "InvoiceCustomerId",true));

现在有一个问题。为什么?当您查看DataBindings集合的Add方法的IntelliSense提示时,您将看到sth。像这样:

Now there is a question. Why ? When you look at the IntelliSense hint for the Add method of the DataBindings collection you see sth. like this:


使用指定的控件属性名称,数据源和数据成员创建System.Windows.Forms.Binding,到集合

Creates a System.Windows.Forms.Binding using the specified control property name, data source and data member, and adds it to the collection

在我看来,在阅读本描述后,上面引用的两行代码的结果应该是漂亮的几乎相同。为什么不是?好吧,让我们希望它只是我们缺乏知识,否则它是一个错误:)

Well in my humble opinion, after reading this description, the result of the two lines of code cited above should be pretty much the same. Why it is not ? Well let's hope it is only our lack of knowledge, otherwise it is a bug:)

这篇关于实体框架4使用外键的WinForms组合框数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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