How to associate an Entity Framework association with a ComboBox?

I am working on an internal software tracking program, and each program version is assigned a lead programmer from the Employee database. My simple model looks like this:

Entity Framework model

I initially had a RevisionBindingSource object associated with my Revisions collection:

 Dim container as new EntityContainer revisionBindingSource.DataSource = container.Revisions ... dgRevisions.DataSource = revisionBindingSource dgRevisions.DataMemeber = "" 

This worked well, and I was able to bind to various required properties, such as the name of the application:

 lblAppTitle.DataBindings.Add("Text",revisionBindingSource,"Application.Title") 

However, I now need a ComboBox, whose elements are tied to the list of employees, and the selected value is tied to the lead programmer of the current version. I tried to create a new employeeBindingSource , but realized that I do not have a binding element for Value :

 employeeBindingSource.DataSource = container.Employees ... cboLead.DataSource = employeeBindingSource cboLead.DisplayMember = "Name.Display" 'Name is a complex type' cboLead.ValueMember = '?? 

So, I rewrote some of my bindings to have only one bindingSource :

 bindingSource.DataSource = container ... dgRevisions.DataSource = bindingSource dgRevisions.DataMemeber = "Revisions" ... cboLead.DataSource = bindingSource cboLead.DisplayMember = "Employees.Name.Display" cboLead.ValueMember = "Employees" ... lblAppTitle.DataBindings.Add("Text",bindingSource,"Revisions.Application.Title") 

It still doesn't even fill the ComboBox with anything.

Which template is better for me to use - two different sources of binding or one? What am I doing wrong in binding my ComboBox? And as soon as my ComboBox fills up, how can I bind the current value to the programmer conducting the audit?

Sorry for the old question and thanks.

+6
source share
1 answer

There is nothing wrong with having more than one source of binding in your form. In fact, source chaining, as you suggest above, can be a convenient strategy.

However, in this situation, there is no link that you will need to fill in to support the binding of the .Value property to the actual EF object: you will need to create a separate class for binding purposes. This method is also very useful when binding to enums.

This method is very common when your EF data model does not quite match the way you want your user interface to work. For WPF (not WinForms, as in this example), this is often referred to as part of the ViewModel. After you do this several times, it will become a second nature.

Here is an example implementation of the class you need to create:

 public class EmployeeBindingObject { public Employee Employee { get; private set; } public string EmployeeName { get { return this.Employee.Name; } } private EmployeeBindingObject(Employee employee) { this.Employee = employee; } /// <summary> /// Gets a binding list for a specified list of Employees. /// </summary> /// <param name="types"></param> /// <returns></returns> public static IBindingList GetBindingList(IEnumerable<Employee> employees) { BindingList<EmployeeBindingObject> result = new BindingList<EmployeeBindingObject>(); foreach (var ee in employees) { result.Add(new EmployeeBindingObject(ee)); } return result; } } 

After creating this class, you must compile and then create a data source (Data → Add new data source ...) for EmployeeBindingObject.

  • Set ValueMember to Employee
  • Set DisplayMember to EmployeeName
  • Set the SelectedValue property to another BindingSource Employee property.
  • Then in your code, you need to initialize the BindingSource binding object as follows:

      employeeBindingObjectBindingSource.DataSource = EmployeeBindingObject.GetBindingList(container.Employees) 
+3
source

Source: https://habr.com/ru/post/894672/


All Articles