This is a migrated thread and some comments may be shown as answers.

[Solved] Binding Concrete types (Inheritance)

1 Answer 86 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Grooverinthesouth
Top achievements
Rank 1
Grooverinthesouth asked on 07 Apr 2009, 04:02 PM
I am currently using the RadGrid and have been differentiating my business objects by properties set in tables.
Example: One Table called 'Contacts' which has a flag column called 'IsPerson' which determines whether the contact is a Person or Company. When binding to the grid I would show or hide columns ( Visible='<%# Eval("IsPerson") %> ) applicable to either type based on whether the 'IsPerson' is true or false, but still have both Persons and Companies in the same bindable collection.
Look at the Code below to understand where I'm coming from:
However I have moved onto the entity framework and have implemented Table-Per-Hierarchy inheritance set out as follows:
abstract class Contact{  
    int ContactID;  
    string Name;  
.......  
}  
 
class Person : Contact{  
    decimal Salary;  
.......  
}  
 
class Company : Contact{  
    decimal ShareValue;  
.......  

To bind I would call
using (MyEntityContext c = new MyEntityContext()){  
    IList<Contacts> contacts = c.Contacts.ToList<Contacts>();  
 
    MyRadGrid.DataSource = contacts;  
    MyRadGrid.DataBind();  
}  
 

How do I differentiate between Person and Company objects in my collection when binding in a Custom GridTemplateColumn?
Example
<itemTemplate>
<%# Eval("Salary") %>    // Works for type 'Person', NOT for type 'Company'
</ItemTemplate>

Please let me know if this doesn't make sense and I'll try and clarify further,,,,

1 Answer, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 10 Apr 2009, 10:59 AM
Hello Grooverinthesouth,

For your convenience I've prepared sample application which demonstrates how you can bind RadGrid to structure described in your post.
Please find the example attached to this post.

Kind regards,
Nikolay
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Grooverinthesouth
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Share this question
or