Hi,
I am having some trouble binding a CollectionClass to GridView where the collection contains entities that are derived from the Generated entities. To give you an example, I've written this toy application using the northwind database. Lets say, I want to add some more properties/fields to the CustomerEntity. I create a class that inhertis from the CustomerEntity and adds some functionality.
public class DecoratedCustomer : ORMTester.NorthWind.EntityClasses.CustomersEntity
{
public DecoratedCustomer(string customerID)
: base(customerID)
{
}
public string LuckyNumber
{
get {
return "13";
/* Custom functionality here. Quite possibly based on any data in the particular customer instance */
}
}
}
I create a custom collection (based on the CustomersCollection class) that can give me these decorated classes
public class AggregatedCustomerCollection : ORMTester.NorthWind.CollectionClasses.CustomersCollection
{
public new System.Collections.IEnumerator GetEnumerator()
{
foreach (ORMTester.NorthWind.EntityClasses.CustomersEntity currentCustomer in base.Items)
{
DecoratedCustomer wrappedUpCustomer = new DecoratedCustomer(currentCustomer.CustomerId);
yield return wrappedUpCustomer;
}
}
}
Here, I am overwriting the enumerator of the customerscollection class to return the DecoratedCustomer instead of the base customer.
And to bind it to the GridView, I am doing the following :
ORMTester.DBLayer.AggregatedCustomerCollection allCustomers = new ORMTester.DBLayer.AggregatedCustomerCollection();
allCustomers.GetMulti(null);
TestGridView.DataSource = allCustomers;
TestGridView.DataBind();
When the bound columns only access the fields in the base customer collection, this works fine. But as soon as I try to access the custom field ("LuckyNumber"), I get an error saying that LuckyNumber is not present
<asp:BoundField DataField="LuckyNumber" HeaderText="LuckyNumber" SortExpression="LuckyNumber" />
This seems to indicate that the AggregateCustomerCollection's enumerator is still returning the base Customer class (or casting whats returned into the base customer class) rather than returning the decorated customer class.
Is there some other interface I should implement or field/type I should set/override to return the decorated customer class? What am I missing/overlooking here?