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

Sorting problem with Inheritance

9 Answers 133 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rafael
Top achievements
Rank 1
Rafael asked on 24 Apr 2009, 01:28 PM

Hi developers,

I'm using a List of objects to fill my Grid. The objects type is a class that inherits from another class. My grid shows the fields correctly, but if I try sort, I get some strange errors. When I try to sort by a field from the base class, I get an error (Cannot find the column '...'). If I try sort by a field from the child class (used as DataSource), the Grid is sorted correctly.

My Telerik version is Q3 2008.

Following, I try to exemplify the situation.

My classes:

public class ClassA  
{  
   public string ID { getset; }  
   public string Name { getset; }  
}  
 
public class ClassB : ClassA  
{  
   public string ZipCode { getset; }  

My Page:

radGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
{  
    List<ClassB> list = getData();  
    radGrid.DataSource = list;  

If I try to sort by ZipCode, the Grid is sorted.
If I try to sort by Name or ID, I get the error "Cannot find column".

9 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Apr 2009, 02:23 PM
Hi Rafael,

I tried the scenario and its working fine in my end. Here's the code which I tried.

Class definitions:
 
public class ClassB:ClassA 
    public ClassB() 
    { 
        // TODO: Add constructor logic here 
    } 
    private string zipCode; 
    public string ZipCode 
    { 
        get 
        { 
            return zipCode; 
        } 
        set 
        { 
            zipCode = value; 
        } 
    } 
public class ClassA 
    public ClassA() 
    { 
        // TODO: Add constructor logic here 
    } 
    private string id; 
    private string name; 
    public string ID { get 
    { 
        return id; 
    } 
    set 
    { 
        id = value; 
    }         
        } 
    public string Name { 
 
        get 
        { 
            return name; 
        } 
        set 
        { 
            name = value; 
        } 
    } 

CS:
 
private void Bind() 
    { 
        ClassB b = new ClassB(); 
        b.ID = "1"
        b.Name = "one"
        b.ZipCode = "onezip"
 
        IList<ClassB> list = new List<ClassB>(); 
 
        list.Add(b); 
 
        ClassB b1 = new ClassB(); 
        b1.ID = "2"
        b1.Name = "two"
        b1.ZipCode = "twozip"
        list.Add(b1); 
        RadGrid1.DataSource = list; 
        //RadGrid1.DataBind(); 
    } 

Thanks,
Shinu.
0
deesnider
Top achievements
Rank 1
answered on 24 Apr 2009, 11:01 PM
How about an interface that implements another interface?  It appears properties with value types do not work (sorting fails), but object properties work?

See this example (customized a Telerik forum example).  I have two interfaces where IInterface2 : IInterface1.  The sorting in the grid fails for ID (defined in Interface1) but works for Child.Name (defined in Interface 1 as well).

using System.Collections.Generic; 
using Telerik.Web.UI; 
 
public partial class _Default : System.Web.UI.Page  
{     
    protected void GridSource(object sender, GridNeedDataSourceEventArgs e) 
    { 
        List<IInterface2> data = new List<IInterface2>(); 
        for (int i = 0; i < 5; i++) 
        { 
            MyObject item = new MyObject { ID = i, Text = "Text " + i, Child = new NestedObject { Name ="Name " + i} }; 
            data.Add(item); 
        } 
 
        RadGrid1.DataSource = data; 
    } 
 
public interface IInterface1 
    int ID { getset; } 
    NestedObject Child { getset; } 
 
public interface IInterface2 : IInterface1 
    string Text { getset; } 
 
public class MyObject : IInterface2 
    public int ID { getset; } 
    public string Text { getset; } 
    public NestedObject Child { getset; } 
 
    public MyObject()  
    {  
        // TODO: Add constructor logic here  
    } 
 
public class SuperClass 
    public int SuperID { getset; } 
     
     public SuperClass()  
    {  
        // TODO: Add constructor logic here  
    } 
public class NestedObject 
    public string Name { getset; } 


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title></title
</head> 
<body> 
    <form id="form1" runat="server"
    <asp:ScriptManager runat="server" ID="SMNGR"
    </asp:ScriptManager> 
    <div> 
        Bound on NeedDataSource 
        <br /> 
        <br /> 
        <telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false" AllowSorting="true" 
            OnNeedDataSource="GridSource"
            <MasterTableView> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Text" HeaderText="Text"
                    </telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn HeaderText="Name" SortExpression="Child.Name"
                        <ItemTemplate> 
                            <%#Eval("Child.Name") %> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
                </Columns> 
            </MasterTableView> 
        </telerik:RadGrid> 
    </div> 
    </form> 
</body> 
</html> 
 

0
thepilsbury
Top achievements
Rank 1
answered on 22 Sep 2009, 12:31 PM
Is there an update to this issue as I am experiencing the same problem with object inheritance and the grid. The behaviour is the same as the first post. The grid displays the values but sorting throws up errors in that it can't find that property. Interestingly, any property defined on the derived object can't be sorted on - the grid seems to only be looking at the object at the top of the hierarchy for the properties when sorting.
0
Veli
Telerik team
answered on 23 Sep 2009, 04:08 PM
Hello,

The general case is as follows: If you have a collection of base class objects, RadGrid works only with base class properties. The same goes for a collection of derived class objects, i.e. both base class and derived class properties should be visible to RadGrid. The GridTableView class now provides a property named RetrieveDataTypeFromFirstItem that causes RadGrid to use the first item type as a template for the properties of all the remaining items in the collection.

Darren, you seem to define a collection of base class objects that contain objects of the derived class. Can you, please, check this out.

We are currently investigating these object binding issues and will keep you posted on the topic.

Best wishes,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
thepilsbury
Top achievements
Rank 1
answered on 24 Sep 2009, 10:11 AM
Hi,

Thanks for your swift reply. 

I am indeed using a collection of base objects to store derived objects - ahhh, the beauty of inheritance means I can do this and is an elegant solution to my business problem. 

I've created a workaround for this by creating a temporary collection of derived objects and then just filling that collection with the items in the base object collection and then assigning that to the radgrid. Not very elegant but it works. 

I'll try the property you suggest.

Thanks
0
Pramit Agrawal
Top achievements
Rank 1
answered on 12 Oct 2012, 09:42 AM
Hi,
I have same problem. I am binding the grid with Parent class and on the Sort it it fails for the properties which belongs to Child class.
I have these classes
 abstract class GridItem
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }
         }
 abstract class CustomGridItem : GridItem
        {
            public int StatusId { get; set; }
            public string Description { get; set; }
        }
 class AGridItem : CustomGridItem 
        {          
            public string Height { get; set; }
            public string Width { get; set; }
            public string Color { get; set; }          
        }

ASPX.cs
 protected void rgGridItems_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            rgGridItems.DataSource = GetGridItems();
        }
 private IEnumerable<GridItem> GetGridItems()
{
 // Implementation
}
I am binding RadGrid with a collection of GridItem type and the Grid has columns like Id, Name, Height, Width, Color defined at design time.
Radgrid is successfully showing data from all the fields/columns, but when I Sort on Height, it says "No property or field Height exists in Type GridItem ".

I have following questions:
1. If Height doesn't belong to GridItem, then why the first time data gets bound with RadGrid and successfully shows data in "Height" column.
2.why it is trying to find Property directly in GridItem, why not in any children.
Note: I have tried the property RetrieveDataTypeFromFirstItem="true/false"  doesn't help.

3.I have tried the same solution by upgrading it to Telerik 2012 latest release, same bug exists.

Thanks

0
Andrey
Telerik team
answered on 17 Oct 2012, 08:12 AM
Hello,

As I explained to you in the support thread you have opened, the current implementation of RadGrid does not support such scenario.

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
Madhu
Top achievements
Rank 1
answered on 07 Jul 2013, 07:52 AM
Hi,
  Can you confirm this feature has been implemented or not ?

Regards
Madhu
0
Andrey
Telerik team
answered on 08 Jul 2013, 11:32 AM
Hi,

The described behavior is a limitation in the design of the control and in order to fix it we need to redesign the control. Currently our Dev team has no plans for such activity.

Excuse us for any inconvenience caused.

Regards,
Andrey
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Rafael
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
deesnider
Top achievements
Rank 1
thepilsbury
Top achievements
Rank 1
Veli
Telerik team
Pramit Agrawal
Top achievements
Rank 1
Andrey
Telerik team
Madhu
Top achievements
Rank 1
Share this question
or