views:

503

answers:

2

I am trying to select certain fields from my entity to be used as the datasource for a datagridview, but I haven't been able to make it work. Is such a thing possible? For example, I have a Customers entity that contains several entityreferences. I want to take fields from the customers entity and from within those entityreferences and display them in the datagridview. I haven't been able to come up with a Linq query to accomplish this, and even when you simply use the entire entity as the datasource the fields within the entityreferences are not displayed. Any idea what I am doing wrong? Thanks for the help.

A: 
from customer in context.customers
select new 
{
    Name = customer.Name,
    City = customer.Address.City
}

that will create a custom object and you can see the second property is referencing an entity field on the primary entity.. basically just transform the data to a new object and bind the enumerable generated to the grid.

sorry if this is a little mumbled, typing on my phone.

Quintin Robinson
A: 

Caveat: This is not tested with entity framework references.

When using object data sources you can reference properties of object references, but you must first cast the object:

<asp:Label ID="lblCity" runat="server" Text='<%# ((Customer)Container.DataItem).Address.City%>'></asp:Label>

Could this be your problem accessing properties of the entity references?

HectorMac