Linq sql data binding to winforms combobox

2019-08-21 04:31发布

问题:

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 isn't working. The ComboBox always insta-binds the FK object to the first item in its list.

Any ideas?

回答1:

i don't know exactly in which post, but I remember that scottgui came up with solution to this problem in linq series...maybe to try to find it:

http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx

cheers



回答2:

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-to-sql