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

Sorting/Filtering on Child Classes

3 Answers 102 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 19 Apr 2012, 07:28 PM
I have a RadGrid that's binding to a list of abstract classes.  However, some properties of some of the child classes have to be exposed in the grid (and left blank if they don't exist):

<telerik:RadGrid runat="server" ID="rgStuff" AllowSorting="true" AllowFilteringByColumn="true" OnNeedDataSource="rgStuff_NeedDataSource" AutoGenerateColumns="False">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn HeaderText="Generic1" DataField="Generic1"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Generic2" DataField="Generic2"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Bob1" DataField="Bob1"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Bob2" DataField="Bob2"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Steve1" DataField="Steve1"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Steve2" DataField="Steve2"></telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Class definitions and binding code:

class Generic
{
    public string Generic1 { get; set; }
    public string Generic2 { get; set; }
}
 
class Bob : Generic
{
    public string Bob1 { get; set; }
    public string Bob2 { get; set; }
}
 
class Steve : Generic
{
    public string Steve1 { get; set; }
    public string Steve2 { get; set; }
}
 
public partial class InheritanceTest : System.Web.UI.Page
{
    protected void rgStuff_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        rgStuff.DataSource = new List<Generic>() {
            new Bob() { Generic1 = "First", Generic2 = "Bob", Bob1 = "Bob1-1", Bob2 = "Bob1-2" },
            new Bob() { Generic1 = "Second", Generic2 = "Bob", Bob1 = "Bob2-1", Bob2 = "Bob2-2" },
            new Steve() { Generic1 = "First", Generic2 = "Steve", Steve1 = "Steve1-1", Steve2 = "Steve1-2" },
            new Steve() { Generic1 = "Second", Generic2 = "Steve", Steve1 = "Steve2-1", Steve2 = "Steve2-2" }
        };           
    }
}

Display of the grid works great.  However, when I attempt to filter or sort on a column that exists in one of the child classes, I get an error:

No property or field 'Bob1' exists in type 'Generic'


Is there a simple way to go about fixing this?  I have a few workarounds in mind (creating a view of the data and binding to that, or adding virtual properties to the Generic class), but I'd rather do this as cleanly and with as little overhead as possible.

Thanks!

3 Answers, 1 is accepted

Sort by
0
Jon
Top achievements
Rank 1
answered on 23 Apr 2012, 08:23 PM
Hate bumping, but does anyone have any thoughts on this?
0
Accepted
Andrey
Telerik team
answered on 25 Apr 2012, 09:12 AM
Hello,

When you set a list of objects of some type as a datasource of RadGrid, it extracts the properties for this type and use these properties for all internal functions - like sorting, filtering, grouping, etc.. So since you set list of Generic objects, RadGrid could use only the properties from the Generic class to sort or filter.

However, what is the purpose of this class inheritance?

 If you are trying to create hierarchical structure you should use the base class in the NeedDataSource and the child classes in the DetailTableDataBind method. Thus you will logically distinguish the top most and the nested levels.

Beside that your both approaches will work for achieving your goal.

Regards,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Jon
Top achievements
Rank 1
answered on 25 Apr 2012, 01:26 PM
The purpose of the inheritance isn't for a hierarchy- in the real application there's several types of projects, with some properties in common and some that are specific to the project type.  The requirements detail a dashboard view of all projects regardless of the type, and many of the columns in the view only exist in one (or more, but not all) of the project types.

Thanks for the info.  I guess I'll do one of my approaches instead.
Tags
Grid
Asked by
Jon
Top achievements
Rank 1
Answers by
Jon
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or