Databinding using Entity Framework 4 on WinForms ComboBoxes with Foreign Keys

c# combobox entity-framework foreign-keys


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.Add("SelectedValue", quoteBindingSource, "CustomerId");

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

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

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

            comboBox6.DataBindings.Add("SelectedValue", quoteBindingSource, "SiteCustomerId");
2/17/2011 6:46:52 AM

Accepted Answer

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

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:

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:)

2/28/2011 8:16:19 AM

Popular Answer

Well your approach is not that bad as you said. First thing: do you need this Clear() statement ? What is the purpose of using it here? The second thing is that you can try to change your code as follows and see if it helps:

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; 

and then in each ComboBox instead of:

comboBox6.DataSource = customersBindingSource;

you do:

comboBox6.DataSource = customers;

Also make sure that the foreign keys are really properly defined for the tables because otherwise you must use the navigation properties instead of referencing to keys while adding DataBinding to ComboBoxes. When it comes to saving, the context has a SaveChanges() method, look it up. Hope it helps.

I recommend Julie Lerman's book on EF4 (second edition) called Programming Entity Framework

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow