Sorting problem with Inheritance

10 posts, 0 answers
  1. Rafael
    Rafael avatar
    5 posts
    Member since:
    Mar 2009

    Posted 24 Apr 2009 Link to this post

    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".
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 24 Apr 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. deesnider
    deesnider avatar
    14 posts
    Member since:
    Nov 2008

    Posted 24 Apr 2009 Link to this post

    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> 
     

  5. thepilsbury
    thepilsbury avatar
    36 posts
    Member since:
    Jan 2007

    Posted 22 Sep 2009 Link to this post

    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.
  6. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 23 Sep 2009 Link to this post

    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.
  7. thepilsbury
    thepilsbury avatar
    36 posts
    Member since:
    Jan 2007

    Posted 24 Sep 2009 Link to this post

    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
  8. Pramit Agrawal
    Pramit Agrawal avatar
    4 posts
    Member since:
    Jun 2012

    Posted 12 Oct 2012 Link to this post

    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

  9. Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 17 Oct 2012 Link to this post

    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.
  10. Madhu
    Madhu avatar
    8 posts
    Member since:
    Jul 2010

    Posted 07 Jul 2013 Link to this post

    Hi,
      Can you confirm this feature has been implemented or not ?

    Regards
    Madhu
  11. Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 08 Jul 2013 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017