Hierarchical Data, Dictionary and List

9 posts, 1 answers
  1. Evaluation
    Evaluation avatar
    4 posts
    Member since:
    Mar 2012

    Posted 22 Mar 2012 Link to this post

    Hello 

    I have been evaluating the Telerik-Reports and so far I find them very good. However, when it comes to hierarchical data, I just can't get the reports to work at all. I made up a simplified example, that would be very typical for our company. Should we be able to generate reports using such a hierarchy as datasource, I could convince my boss to purchase Telerik-Reports ;)

    I have been trying with subreports as well as groups and tables. However, I can not get it to display any of the sub-classes if they contain either a dictionary or a list. I think the problem is, that it won't recognize the DataSource correctly.

    Background for the problem:
    I have a list with people (drivers) and I would want to display a report where you can see on what days the people worked and how much work they have done. 

    I use the free trial version (Q1_2012_v6_0_12_302) and .NET 4.0 and I am certain that my list contains valid test data. Note, that I have a dictionary as well as lists inside the Classes.

    List<DriverDto> drivers;    // this would be the DataSource

    // the corresponding data structure
    public class DayDto
    {
        public DateTime Day { get; set; }
        public int CalendarWeek { get; set; }
        public int Work { get; set; }
        public int Break { get; set; }
    }

    public class YearDto
    {
        public List<DayDto> Days { get; set; }
        public string Year { get; set; }

        public YearDto()
        {
            Days = new List<DayDto>();
        }
    }

    public class DriverDto
    {
        public string Surname { get; set; }
        public string FirstName { get; set; }
        public Dictionary<int, YearDto> Years { get; set; }

        public DriverDto()
        {
            Years = new Dictionary<int, YearDto>();
        }
    }

    Desired outcome:
    The report should look something like this.

    =============================================
    Peter Parker
    =============================================
    2011
    ------------------------------------------------------------------------
    January
    Date                                 Work (Hours)      Break (Hours)
    2011.01.01                         2.5                     1.0
    2011.01.02                         2.0                     0.5
    Total CalendarWeek 52    4.5      1.5

    2011.01.03   3.5                     0.7
    2011.01.04                         1.0                     0.2
    2011.01.05                         3.0                     0.2
    2011.01.06                         2.3                     0.5
    2011.01.07                         2.5                     0.2
    2011.01.08                         2.7                     0.5
    2011.01.09                         3.2                     0.2
    Total CalendarWeek 01     18.2                    2.5

    2011.01.10                         3.5                      0.7
    2011.01.11                         1.0                      0.2
    2011.01.12                         3.0                      0.2
    2011.01.13                         2.3                      0.5
    2011.01.14                         2.5                      0.2
    2011.01.15                         2.7                      0.5
    2011.01.16                         3.2                      0.2
    Total CalendarWeek 02     18.2                     2.5

    Total January                      40.9                    6.5
    ------------------------------------------------------------------------

    =============================================
    Bruce Banner
    =============================================
    2011
    ------------------------------------------------------------------------
    January
    Date                                 Work (Hours)      Break (Hours) 
    2011.01.01                         2.5                    1.0
    2011.01.02                         2.0                    0.5
    Total CalendarWeek 52      4.5                    1.5

    2011.01.03                         3.5                     0.7
    2011.01.04                         1.0                     0.2
    2011.01.05                         3.0                     0.2
    2011.01.06                         2.3                     0.5
    2011.01.07                         2.5                     0.2
    2011.01.08                         2.7                     0.5
    2011.01.09                         3.2                     0.2
    Total CalendarWeek 01     18.2                    2.5

    2011.01.10                         3.5                     0.7
    2011.01.11                         1.0                     0.2
    2011.01.12                         3.0                     0.2
    2011.01.13                         2.3                     0.5
    2011.01.14                         2.5                     0.2
    2011.01.15                         2.7                     0.5
    2011.01.16                         3.2                     0.2
    Total CalendarWeek 02     18.2                    2.5

    Total January                     40.9                    6.5
    ------------------------------------------------------------------------ 

    2012
    ------------------------------------------------------------------------ 
    January
    Date                                 Work (Hours)      Break (Hours) 
    2011.01.01                         2.5                     1.0
    Total CalendarWeek 52     2.5                      1.0

    2011.01.02                         2.0                     1.0
    2011.01.03                         2.0                     1.0
    2011.01.04                         2.5                     1.5
    2011.01.05                         2.0                     1.5
    2011.01.06                         2.0                     1.0
    2011.01.07                         2.0                     1.5
    2011.01.08                         2.5                     1.5
    Total CalendarWeek 01     15.0                    9.0

    Total January                     20.0                   10.0
    ------------------------------------------------------------------------ 

    Any hint about how to achieve this would be greatly appreciated.
  2. Elian
    Admin
    Elian avatar
    435 posts

    Posted 26 Mar 2012 Link to this post

    Hello,

    I have attached an application that shows one possible way of utilizing nested collections as data-sources. The List< > of parent objects is bound to the report itself. In the detail section there is a Table item which shows the nested collection data. Its DataSource is set with the usage of Bindings to the List< > of child objects.
    If you decide that you need SubReports (instead of Tables), you can use exactly the same approach (with Bindings) to set the SubReport's DataSource. 

    Regards,
    Elian
    the Telerik team
    NEW in Q1'12: Telerik Report Designer (Beta) for ad-hoc report creation. Download as part of Telerik Reporting Q1 2012. For questions and feedback, use the new Telerik Report Designer Forum.
  3. Evaluation
    Evaluation avatar
    4 posts
    Member since:
    Mar 2012

    Posted 26 Mar 2012 Link to this post

    Thank you for your answer.

    I can bind the data in a simplified manner as you showed it in your attached file. Unfortunately it won't work in the case I asked earlier. I will try to be more specific. If I only have a dictionary (e.g. Dictionary<int, string>) I can use it as DataSource for a report and access it like [=Fields.Item.Key] and [=Fields.Item.Value].
    However, If I have a class containing a dictionary and the dictionary contains some subclass rather than a primitive datatype (e.g. Dictionary<int, someClass>) I can not use the dictionary as a DataSource for either a table or a subreport. I am guessing that I am missing something rather basic about the syntax. I thought that I should be able to access the dictionary much like a list of the form List<KeyValuePair<int, someClass>> but that doesn't seam to work. I provided the most simple datastructure which demonstrates the problem in my initial post.

    Furthermore, in my case the class within in the dictionary contains a list. Currently I don't know if I will be able to group the list and display some totals for each group, as I can not access the dictionary to begin with.

    p.s. this is how I generate the testData, if it helps.
    public class TestDataGenerator
    {
        public List<DriverDto> GetDrivers()
        {
            List<DriverDto> drivers = new List<DriverDto>();
            string[] names = { "Super", "Green", "Spider", "Wonder", "Silver" };
            string[] firstNames = { "Man", "Hulk", "Man", "Woman", "Surfer" };
            Random rand = new Random();

            for (int y = 0; y < names.Length; ++y)
            {
                DriverDto driverDto = new DriverDto();
                driverDto.Surname = names[y];
                driverDto.FirstName = firstNames[y];

                DateTime startDate = new DateTime(2012, 01, 01);
                for (int i = 0; i < 365; ++i)
                {
                    DayDto dayDto = new DayDto();
                    dayDto.Day = startDate.AddDays(i);
                    dayDto.Work = rand.Next(60, 120);
                    dayDto.Break  = rand.Next(60, 120);

                    CultureInfo culture = CultureInfo.CurrentCulture;
                    dayDto.CalendarWeek = culture.Calendar.GetWeekOfYear(dayDto.Day, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

                    if (!driverDto.Years.ContainsKey(dayDto.Day.Year))
                        driverDto.Years.Add(dayDto.Day.Year, new YearDto());

                    driverDto.Years[dayDto.Day.Year].Year = dayDto.Day.Year.ToString();
                    driverDto.Years[dayDto.Day.Year].Days.Add(dayDto);
                }

                drivers.Add(driverDto);
            }

            return drivers;
        }
    }

    Like I said it seams to be a problem related to the datastructure in my initial question. Please refer to the initial post for details about the datastructure.

    Again, thanks for your help. Any further hints are very much appreciated.
  4. Answer
    Elian
    Admin
    Elian avatar
    435 posts

    Posted 28 Mar 2012 Link to this post

    Hi,

    We looked into your question and it appears that there are some difficulties when binding the DataSource to Dictionary<int, T> (when the key is Int). If the Dictionary Key is anything other than Int, it would work as expected (you can bind it like any other collection and use it with =Fields.Key / Fields.Value.  

    We will try to fix it for the internal build, which will be available for download by the end of the week. 

    Kind regards,
    Elian
    the Telerik team
    NEW in Q1'12: Telerik Report Designer (Beta) for ad-hoc report creation. Download as part of Telerik Reporting Q1 2012. For questions and feedback, use the new Telerik Report Designer Forum.
  5. Evaluation
    Evaluation avatar
    4 posts
    Member since:
    Mar 2012

    Posted 29 Mar 2012 Link to this post

    Hi,

    I switched the key to another type (string) and it works indeed. I did not expect to find a bug with my first question :)

    This is not related to my initial question, but I think that I found yet another bug. If I have a table and put a list into one of the table-cells it seams impossible to remove the list later. I have to remove the entire table and rebuild it from scratch. I am wondering, is there a bug report section where we can submit bugs?

    Anyway, I consider my initial question as answerd.
    Thank you for your feedback. Your help is very much appreciated!
  6. Elian
    Admin
    Elian avatar
    435 posts

    Posted 29 Mar 2012 Link to this post

    Hello,

    The behaviour you are describing is normal because the table cannot be left without a cell (cells can only be replaced). However, the case with List/Panel is more delicate because the designer has to allow you to drop items inside the newly created Panel (the List also contains Panel inside) and it cannot know whether you want to place items in the Panel or replace the whole Panel. This is why you are unable to change the cell content any more. The only way to replace the cell is to do it in the code (open Report.Designer.cs) locate the cell content and replace it manually. 

    All the best,
    Elian
    the Telerik team
    NEW in Q1'12: Telerik Report Designer (Beta) for ad-hoc report creation. Download as part of Telerik Reporting Q1 2012. For questions and feedback, use the new Telerik Report Designer Forum.
  7. Evaluation
    Evaluation avatar
    4 posts
    Member since:
    Mar 2012

    Posted 29 Mar 2012 Link to this post

    Hi

    I will try to do this in the Report.Deigner.cs. Everything else seams to work very well and I consider this topic as answerd.

    Thanks for your help and feedback.
  8. חוי
    חוי avatar
    1 posts
    Member since:
    Oct 2014

    Posted 27 Oct 2014 in reply to Elian Link to this post

    how can I get after this bindings a property from the parent?
    If I have a Person object and for each person I have List of<Book>.
    now I use the binding option to set the datasource of my table to my list
    but I want to show in that table the personName too!
    how can I get it?
  9. Stef
    Admin
    Stef avatar
    3610 posts

    Posted 29 Oct 2014 Link to this post

    Hello,

    You can use the item's parent DataObject and get the property e.g.:
    =ReportItem.Parent.DataObject.personName

    For more details, please check the How to use the ReportItem.DataObject property in expressions article.

    Regards,
    Stef
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top