Entity Framework 4 Databinding on WinForms ComboBoxes with Foreign Keys

谁说胖子不能爱 提交于 2019-12-11 04:02:20

问题


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

回答1:


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:


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; 

to

.......
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



来源:https://stackoverflow.com/questions/5025880/entity-framework-4-databinding-on-winforms-comboboxes-with-foreign-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!