none
reflection + get property of a class which is property of another class RRS feed

  • Question

  • Hi,

    We are trying to export telerik radgrid rows to excel. We are looping through rows and trying to get columns. For this reflection is used. Below is the code

    Grid datasource is an object with one of the property being another class like this:

    eg:

                           

    public class summary
    {

       private int _trnID = 0;
       private Client _clientDetails;

    public int TrnID
    {
    get{ return _trnID;}
    set{ _trnID = value;}
    }

    public Client ClientDetails
    {
    get{ 
    return _clientDetails;
       }
    set{ _clientDetails;= value;}
    }

    }

    public class Client
    {
       private short _clientID;
       private string _name;

    public short ClientID
            {
                get { return _clientID; }
                set { _clientID = value; }
            }

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
    }

    This class summary gets populated from database and used as datasource for grid.
    Like this:
    <telerik:GridBoundColumn DataField="TrnID" DataType="System.Int32" HeaderText="ID" UniqueName="TrnID"

                             HeaderTooltip="Transaction ID"  />
            <telerik:GridBoundColumn DataField="ClientDetails.Name" HeaderText="Client" UniqueName="ClientDetails.Name"                                 HeaderTooltip="Client" HeaderStyle-Width="6%" />

    When I am exporting to excel, this is what I am doing:

    protected void CreateExcelWorkBook(RadGrid pRadGrid)
    {
    List<PropertyInfo> properties = new List<PropertyInfo>();
    int cellIdx = 0;

                for (int colIdx = 0; colIdx < pRadGrid.Columns.Count; colIdx++)
                {
                    GridColumn gridCol = pRadGrid.Columns[colIdx];
    if (gridCol.Visible && gridCol.Exportable && gridCol.Display)
                    {
                        properties.Add(typeof(T).GetProperty(((GridBoundColumn)gridCol).DataField));
                    }
        }
    }
    Above code properties.Add(typeof(T).GetProperty(((GridBoundColumn)gridCol).DataField)); works fine for TrnID property and I get TrnID as propertyname.
    When it loops through and goes to ClientDetails property, it is null becasue unlike 'TrnID', it is 'ClientDetails.Name'.
    How can I get property of object with in object.

    Thank You


    Friday, June 7, 2019 3:33 PM

All replies

  • Hello,

    Have you tried asking in their forums?

    https://www.telerik.com/forums/aspnet-ajax

    https://www.telerik.com/support/winforms

    Also you should check their code samples too.

    For example export from a RadGrid to Excel

    https://demos.telerik.com/aspnet-ajax/grid/examples/functionality/exporting/excel-export/defaultcs.aspx


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, June 7, 2019 5:20 PM
    Moderator
  • Hi Thanks for response. It has nothing to do with Telerek Radgrid. My question is about using generic list, reflection and hot to access property of a child class which is property in master class.

    Thank You

    Friday, June 7, 2019 5:26 PM
  • To get a property within a property you have to manually get the parent property, then get its value and then use the value to get the next level of properties.

    class Program
    {
        static void Main ( string[] args )
        {
            var root = new Folder() { Name = @"C:\" };
            var temp = new Folder() { Name = "Temp", Parent = root };
            var child = new File() { Name = "Test.txt", Parent = temp };
                
            DisplayProperties(child);
        }
    
        static void DisplayProperties ( object root, int indent = 0 )
        {
            var rootType = root.GetType();
    
            var properties = rootType.GetProperties();
            foreach (var property in properties)
            {
                var propertyType = property.PropertyType;
    
                //Get the property value, if any
                var value = property.GetValue(root);
    
                //Print the property value
                Console.WriteLine($"{"".PadLeft(indent*2)}{propertyType.Name} {property.Name} = {value}");
    
                //Recursively print the value's properties if not a primitive
                if ((value != null) && !propertyType.IsPrimitive && (propertyType != typeof(string)))
                {
                    DisplayProperties(value, indent + 1);
                };
            };
        }
    }
    
    class Folder
    {
        public Folder Parent { get; set; }        
    
        public string Name { get; set; }
    }
    
    class File
    {
        public Folder Parent { get; set; }
    
        public string Name { get; set; }
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 7, 2019 6:13 PM
    Moderator
  • You probably have to split the name using “…DataField.Split(‘.’)”, then organise the corresponding loops. The type of properties can be determined from PropertyInfo.PropertyType. The properties list probably will be replaced with a list of list of PropertyInfo. The function that extract the values will need some adjustments.

    But, if you have a populated visible or invisible grid, maybe you can extract the data from the grid’s cells.

    • Edited by Viorel_MVP Friday, June 7, 2019 6:54 PM
    Friday, June 7, 2019 6:50 PM
  • Hi Spunny,

    Thank you for posting here.

    Based on your description, you want to get ClientDetails property by using reflection.

    I have tested the code, it works well. I think ((GridBoundColumn)gridCol).DataField) may be not equal to ClientDetails, which causes the error. It is best for you to check it.

    Tested code:

    List<PropertyInfo> properties = new List<PropertyInfo>();
                properties.Add(typeof(summary).GetProperty("TrnID"));
                properties.Add(typeof(summary).GetProperty("ClientDetails"));

    Result:

    Meanwhile, I have to mention that something is wrong with the following code you provided.

    public Client ClientDetails
    {
    get{ 
    return _clientDetails;
       }
    set{ _clientDetails;= value;}
    } 
    
    }

    It should be like the following code.

    public Client ClientDetails
            {
                get
                {
                    return _clientDetails;
                }
                set { _clientDetails= value; }
            }
    

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 10, 2019 2:44 AM
    Moderator
  • Hi 

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 25, 2019 8:42 AM
    Moderator