【问题标题】:Entity Framework 4 Databinding on WinForms ComboBoxes with Foreign Keys带有外键的 WinForms ComboBox 中的实体框架 4 数据绑定
【发布时间】:2011-06-28 21:26:36
【问题描述】:

我正在使用 Entity Framework 4,我需要获取 WinForms 以将客户和报价绑定为主 - 细节关系。

我有一个报价表,我在 Windows 窗体上作为详细信息视图进行布局。

报价表有 3 个指向客户表的外键。 (字段 CustomerId、SiteCustomerId、InvoiceCustomerId 都链接到 Customer 表中的 Id 字段)。

在表单上有 3 个客户面板,组合框中的客户名称字段和文本框中的其他客户详细信息字段。

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

我的(坏的)尝试:

     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");

【问题讨论】:

    标签: c# entity-framework combobox foreign-keys


    【解决方案1】:

    感谢您提供 WinForms 项目版本:) 好吧,它发生了,要么有问题,要么我们可能发现了某种错误。在这种情况下,假设我们缺乏某种知识通常更安全。解决方案很简单但很奇怪:在您执行的每个 Combobox 中:

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

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

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

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

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

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

    【讨论】:

    • 确实有帮助。谢谢托比亚斯。
    • 嘿,那么很抱歉将它迁移到 WinForms 时遇到了麻烦......但请记住......永远没有足够的培训 :)
    • 你好。感谢你的帮助。我能再打扰你一下吗?
    • 你好,1天后我就能看,明天还有很多工作,我刚刚看了你的消息。
    • 你好安德鲁。我非常抱歉,但最近工作中的事情一直很艰难。可能你已经设法解决了这个问题,但无论如何我都会解释它。首先是:查看您的数据模型,您可以看到客户和邮政编码之间没有连接(导航属性),这就是为什么州、郊区和邮政编码没有正确绑定到表单控件的原因。一个快速的做法是:if (!(quoteBindingSource.Current as Quote).CustomerId.HasValue) e.Binding.Control.Text = ""; 在绑定到 Addr、Phone 等文本框的 BindingComplete 事件的方法中。
    【解决方案2】:

    嗯,你的方法并没有你说的那么糟糕。第一件事:你需要这个 Clear() 语句吗?在这里使用它的目的是什么?第二件事是您可以尝试按如下方式更改代码,看看是否有帮助:

    quoteBindingSource.DataSource = Context.Quote;
    customersBindingSource.DataSource = Context.Customers; 
    

    .......
    List<Quote> quotes;
    List<Customer> customers;
    .....
    quotes = Context.Quote.ToList();
    customers = Context.Customers.ToList(); 
    .....
    quoteBindingSource.DataSource = quotes;
    customersBindingSource.DataSource = customers; 
    

    然后在每个 ComboBox 中代替:

    comboBox6.DataSource = customersBindingSource;
    

    你会的:

    comboBox6.DataSource = customers;
    

    还要确保确实为表正确定义了外键,否则在将 DataBinding 添加到 ComboBox 时,您必须使用导航属性而不是引用键。说到保存,上下文有一个 SaveChanges() 方法,查一下。希望对您有所帮助。

    我推荐 Julie Lerman 的关于 EF4 的书(第二版),名为 Programming Entity Framework

    【讨论】:

    • 谢谢托拜厄斯。很高兴我在正确的轨道上。如果我单击 1 个组合框,则所有 3 个组合框的显示值都相同。如果我然后单击第二个组合,所有显示值都会更改为第二个组合值。有 3 个外键都指向 Customer 表中的 Id 是否非法?
    • 无论如何这并不违法。我的错。尝试为其他 2 个 ComboBoxes 添加单独的 BindingSource 并将其 DataSources 设置为 2 个其他新 Lists 与第一个具有相同内容的 Context ,修改 ComboBoxes DataSources 应该没问题。
    • 组合框现在是独立的。但是,当我更改一个值然后将注意力集中在另一个控件上时,显示的值会恢复到以前的值。
    • 嗯,如果您不提供负责移动引号的代码,很难判断。我使用了一个示例数据库,其中有一个表,其中一个表有 2 个外键,另一个表有 2 个外键,我将此主表的绑定源连接到绑定导航器,一切正常(当我移动到主表中的另一行,在您的情况下为配额,我可以更改组合中的值,并且在选择另一个后它们不会变回)
    • code和db在excelthoughts.com/code/QM%20-%202010.rar是一个200KB的文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    相关资源
    最近更新 更多