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

Failure on add a group to a Report programatically

3 Answers 339 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joseph
Top achievements
Rank 1
Joseph asked on 21 Dec 2011, 06:15 PM
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

3 Answers, 1 is accepted

Sort by
0
Accepted
Elian
Telerik team
answered on 23 Dec 2011, 01:48 PM
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!

0
Fauzan
Top achievements
Rank 1
answered on 03 Jan 2019, 03:50 AM
why it's null, when i tried your code... only empty space, like invisible table
0
Silviya
Telerik team
answered on 07 Jan 2019, 08:11 AM
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
Tags
General Discussions
Asked by
Joseph
Top achievements
Rank 1
Answers by
Elian
Telerik team
Fauzan
Top achievements
Rank 1
Silviya
Telerik team
Share this question
or