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.)
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!