I am using Entity Framework 4, I need to get WinForms to bind Customers and Quotes as a Master – Detail relationship.
I have a Quote table which I lay out as details view on a Windows Form.
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).
On the form there are 3 Customer panels with the Customer Name fields in a ComboBox, and other Customer detail fields in textBoxes.
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.
My (bad) attempt:
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");
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:
What you should do to improve the situation with values coming back to old ones is:
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:
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:)