Failure on add a group to a Report programatically

4 posts, 1 answers
  1. Joseph
    Joseph avatar
    5 posts
    Member since:
    Aug 2012

    Posted 21 Dec 2011 Link to this post

    Hi There,
    I wanted to add grouping programmatically in my report (the idea is to use one report with different data, one grouped and the other is not).

    I followed the code from the documentation "Adding a group to a report programatically" but It didn't work. I got an error "Table Body has 1 rows but 2 are expected.". 

    I created a simple report for your reference (I don't want to share the actual report), could you let me know what's wrong.

    Here is the sample report.

    Many thanks
  2. Answer
    Elian
    Admin
    Elian avatar
    435 posts

    Posted 23 Dec 2011 Link to this post

    Hi,

    The article is about adding groups to the report. You have created a table and are trying to add table groups to the table. The two things are entirely different. If you follow this article you should have no table (you should put text boxes inside the detail section). 

    About the error you get, you get it because the table must have as many rows as the row groups are(you are adding second group, so the table should have 2 body rows.)

    _table.Body.Rows.Add(new TableBodyRow(Unit.Pixel(50)));
    //This is how you add a body row

    However with this layout of the table you won't get any meaningful result. To get an actual grouping the AgeGroup should be a parent  group to the detail group (the one that shows the data). Meaning you need to nest the groups (then you won't need another body row).

    Your grouping now looks like this:
    Detail                     (2 rows)
    AgeGroup

    It should be like this (in order to properly work):
    AgeGroup                        (1 row)
    ------> Detail (nested)

    However if you add the grouping, you will need to change the layout of the table (so group headers appear).

    My suggestion is the following. Create the table with the grouping in the designer entirely, then go and examine the code in the InitializeComponent() method. This way you will be able to alter the table according to your needs.

    Here is your code a bit reworked that will do the grouping but without any visual improvements (only the data will be grouped):
    public SampleReport()
            {
                InitializeComponent();
     
                _table.DataSource =
                    new List<Person>
                        {
                            new Person{Age = 20, Name = "Test1"},
                            new Person{Age = 30, Name = "Test2"},
                            new Person{Age = 20, Name = "Test3"},
                            new Person{Age = 40, Name = "Test4"},
                            new Person{Age = 20, Name = "Test5"},
                            new Person{Age = 20, Name = "Test6"},
                            new Person{Age = 20, Name = "Test7"},
                            new Person{Age = 30, Name = "Test8"},
                        };
     
                bool youWantGrouping = true;
                if (youWantGrouping)
                {
                    TableGroup group = new TableGroup { Name = "AgeGroup" };
                    group.Groupings.Add(new Telerik.Reporting.Data.Grouping("=Fields.Age"));
     
                    // If you need to order the members of the group, apply sorting
                    //group.Sortings.Add(new Telerik.Reporting.Data.Sorting("=Fields.Country", Telerik.Reporting.Data.SortDirection.Asc));
     
                    //this line will give you
                    //"object reference not set to an instance" exception
                    //if you want group headers, you will have to change the layout of the table               
                    //group.ReportItem = ...;
     
     
                    //Remove the detail group
                    var detailGroup = _table.RowGroups[0];
                    _table.RowGroups.Clear();
     
                    //Add the detail group as a child to the AgeGroup (nest it)
                    group.ChildGroups.Add(detailGroup);
     
                    //Add the AgeGroup
                    _table.RowGroups.Add(group);
                }
            }

    All the best,
    Elian
    the Telerik team

    Q3’11 of Telerik Reporting is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!

  3. Fauzan
    Fauzan avatar
    1 posts
    Member since:
    Dec 2018

    Posted 02 Jan in reply to Elian Link to this post

    why it's null, when i tried your code... only empty space, like invisible table
  4. Silviya
    Admin
    Silviya avatar
    320 posts

    Posted 07 Jan Link to this post

    Hello Fauzan,

    What is the currently installed version of Telerik Reporting? Because during the years, we constantly improve our product and it might be related to API changes introduced. More information could be found in Telerik Reporting Release History and Upgrade Path articles.

    If you need to add group to the Report itself, please refer to Adding a group to a Report programatically section of the help article.
    Otherwise, if you need to add group, please refer to Adding a group to Table data item programatically section.

    In general, our recommendation is to create reports with our dedicated Report Designers. A .NET class report can be created with the Visual Studio Report Designer.
    The designer will auto generate the code of the Report class into its InitializeComponent() method.

    I suggest to take a look at our demo report ProductSales (its InitializeComponent method) as an example of programmatically created report containing Crosstab with groups. It can be found in the ProductSales.Designer.cs file.The report itself is in the solution with our examples by default in Telerik Reporting installation folder, for example C:\Program Files (x86)\Progress\Telerik Reporting <VERSION>\Examples\CSharp\ReportLibrary\ProductSales.

    Regards,
    Silviya
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top