When using BoundFIelds and domain objects... BoundFields use some algo that can't traverse the object hierarchy.
What am I talking about? Take for instance a Northwind database, with certain objects mapped to data tables:
public class Customer
{
public int CustomerId;
public IList<Order> Orders;
}
When binding to a list of Orders in a gridview:
int customerId = int.Parse(selCustomer.SelectedValue);
IList<Customer> customersList = CustomerService.GetCustomersWithOrders();
this.GridView1.DataSource = customersList;
this.GridView1.DataBind();
<asp:GridView runat=server id=GridView1>
<Columns>
<asp:BoundField DataField=CustomerId />
<asp:BoundField DataField=Orders.Count /> <!-- CAN'T WORK -->
<asp:TemplateField><ItemTemplate><%# Eval("Orders.Count") %></ItemTemplate></asp:TemplateField> <!-- DOES WORK -->
</Columns>
</GridView>
I keep posing the question to ASP.Net Program Manager ScottGu, but can't seem to get a response. A feedback item was created to address this, yet it seems very low on the priority list.
In all my experience, I cannot understand why such an important feature, needed by anybody using an actual domain model with objects, cannot use BoundFields out-of-the-box. A "workaround" is to use the TemplateField and Eval... but thats just a poor hack. You don't get any SortExpression support. Before ASP.Net AJAX Extensions, you couldn't even use the ClientSide callback framework with GridViews that contain TemplateFields. You'd get an error saying TemplateFields weren't supported... pfft!
This is really disappointing... I'm using NHibernate to do the OR/Mapping and when I pull these fully hydrated objects from NHibernate queries, I still have to find a way to "flatten" the objects before they hit GridViews. LINQ has this ability, but I'm not cleared to use LINQ on this current project.
I say we need to step up the level of discontent with certain features... If enough people complain and validate the problem (via the feedback item) hopefully it'll get fixed in the next 10 years.
In the interim, has anybody recoded BoundFields (perhaps using Mono source code as a template?) to properly evaluate nested Properties on objects?
Has anybody found a decent solution around this problem?
UPDATE: I'm creating a feedback item to re-address this. Link will be here.