Hello,
I have an Entity, Company, that has a property exposing a list of child entities, Car.
For a quick run I created an instance of my Entity:
Company company = new Company() { Name = "Ford", City = "Detroit" };
and then initialized the child entity collection and added two children:
company.Car = new System.Data.Objects.DataClasses.EntityCollection<Car>();
company.Car.Add( new Car() { Name = "Mustang" } );
company.Car.Add( new Car() { Name = "Explorer" } );
I manually defined the schema with my first Band with a Key of, "Company", and the child band to a Key of, "Car".
The parent record is visible, but the child is not. How should I be doing this?
Thank you,
Blu
It appears to me that the EntityCollection doesn't support any of the required interfaces that would mark it as a child list by the DotNet BindingManager.
In order for this to work, the list of cars must implement IList or IBindingList.
It does implement IListSource, which is interesting. This seems to imply that the EntityCollection contains multiple lists. This makes it unsuitable to display as a set of child bands, but theoretically, you could bind the grid directly to the Cars collection, then, as long as you specified a DataMember as well as a DataSource.
The entity collection column type was set by the designer when I populated the edmx from the database.
Here is the class declaration from looking at the assembly with Reflector:
public sealed class EntityCollection<TEntity> : RelatedEnd, ICollection<TEntity>, IEnumerable<TEntity>, IEnumerable, IListSource where TEntity: class, IEntityWithRelationships
The grid's datasource is a List<Company> with more then one company. Each company has Car instances added to the Car property.
I did not need to instantiate the Car property with a new List, it has an empty List to begin with.
I add the Companies and their Cars prior to binding the Grid, I use: this.ResultsGrid.DataSource = companies;
Setting the ViewStyle on the grid is unlikely to help, since MultiBand is the default. So this would only be a problem if you had already set this property to SingleBand.
What exactly is System.Data.Objects.DataClasses.EntityCollection? I couldn't find this type.I assume it implements IList<> or IBindingList<>, so that should be okay.
What is the grid's DataSource in this scenaria? Is is a single Company? Or a list of companies?
What does the Car property return before you set it to a new EntityCollection? Is it returning an empty list or is it null?
Are you binding the grid before or after adding the Cars?
I commented out: company.Car = new System.Data.Objects.DataClasses.EntityCollection<Car>();
I changed the view style in Grid_InitializeLayout and it did not add the + symbol for the child rows.
private void Grid_InitializeLayout( ...
{
...
e.Layout.ViewStyle = ViewStyle.MultiBand;
}
If you don't see the "+" sign beside each row, yu need to write:
grid.DisplayLayout.ViewStyle = ViewStyle.MultiBand
I think you also shouldn't always construct a new collection for Car. When you make a new Company you can start adding Cars without constructing its collection first. If you want to make sure there is nothing in the collection then use Clear(). This is very important when you bind the collection. If you construct a new collection you might lose the binding.