SQL数据绑定到WinForms组合框

SQL数据绑定到WinForms组合框

本文介绍了Linq SQL数据绑定到WinForms组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将ComboxBox绑定到外键表.我知道on不能绑定到实际的外键SomeTableClassFK(当FK已经具有值时会导致LinqSql异常).因此,我绑定到FK对象SomeTable,通过ComboBox的SelectedItem属性进行引用.

SomePrimaryTable
SomeTableClassFK ---> SomeTableClass.PK
SomeTable --------> SomeTableClass

I am trying to bind a ComboxBox to a foreign key table. I know that on cannot bind to the actual foreign key, SomeTableClassFK, (causes a LinqSql exception when the FK already has a value). So, I am binding to FK object, SomeTable, reference thru the SelectedItem property of the ComboBox.

SomePrimaryTable
SomeTableClassFK ---> SomeTableClass.PK
SomeTable --------> SomeTableClass

this.aComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem",this.somePrimaryTableBindingSource,"PendingReason",false));

this.aComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.somePrimaryTableBindingSource, "PendingReason", false));

这不起作用. ComboBox始终将FK对象绑定到其列表中的第一项.

This isn't working. The ComboBox always insta-binds the FK object to the first item in its list.

有什么想法吗?

推荐答案

好...问题被追溯到时髦的winform行为.显然,不能保证用户控件的OnLoad不会被多次调用.结果,所涉及的两个表(请参见上图)是从不同的DataContext中加载的.啪!

Ok...Issue was traced to funky winform behavior. Evidently, there is no guarantee that the OnLoad for a user control will not be called more than once. As a consequence, the two tables involved, (see diagram above), were being loaded from different DataContexts. Splat!!

这篇关于Linq SQL数据绑定到WinForms组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:22