ComponentOne Reports for WinForms Designer Edition: ComponentOne Reports for WinForms > Working with C1PrintDocument > Data Binding > Data Binding Examples > Using Aggregates

Using Aggregates

The code below expands on the previous example by introducing aggregates in groups and the document as a whole:

      Visual Basic

' Create a Render area to be repeated for each group:

Dim ra As C1.C1Preview.RenderArea = New C1.C1Preview.RenderArea

ra.Style.Borders.All = New C1.C1Preview.LineDef("2mm", Color.Blue)

ra.DataBinding.DataSource = MyData.Generate(20, 0, True)

ra.DataBinding.Grouping.Expressions.Add("Fields!GroupId.Value")

 

' Make an aggregate that will calc the sum of IntValue fields within each group:

Dim agg As C1.C1Preview.DataBinding.Aggregate = New C1.C1Preview.DataBinding.Aggregate("Group_IntValue")

 

' Define the expression that will calc the sum:

agg.ExpressionText = "Fields!IntValue.Value"

 

' Specify that aggregate should have group scope:

agg.Running = C1.C1Preview.DataBinding.RunningEnum.Group

 

' Specify the data source for the aggregate:

agg.DataBinding = ra.DataBinding

 

' Add the aggregate to the document:

doc.DataSchema.Aggregates.Add(agg)

 

' Make an aggregate that will calc the sum of IntValue fields over the whole document:

agg = New C1.C1Preview.DataBinding.Aggregate("Total_IntValue")

 

' Define the expression to calc the sum:

agg.ExpressionText = "Fields!IntValue.Value"

 

' Specify that aggregate should have document scope:

agg.Running = C1.C1Preview.DataBinding.RunningEnum.All

 

' Specify the data source for the aggregate:

agg.DataBinding = ra.DataBinding

 

' Add the aggregate to the document:

doc.DataSchema.Aggregates.Add(agg)

 

' Make the group header:

Dim rt As C1.C1Preview.RenderText = New C1.C1Preview.RenderText

rt.Text = "GroupId: [Fields!GroupId.Value]"

rt.Style.BackColor = Color.Yellow

ra.Children.Add(rt)

 

' This render text will print group records; as can be seen, group aggregate values can be referenced not only in group footer but also in group header and in group detail:

rt = New C1.C1Preview.RenderText

rt.Text = "GroupId: [Fields!GroupId.Value]\rIntValue:[Fields!IntValue.Value]\rGroup_IntValue:[Aggregates!Group_IntValue.Value]\rTotal_IntValue:[Aggregates!Total_IntValue.Value]\rTatalNested_IntValue:[Aggregates!TatalNested_IntValue.Value]"

rt.Style.Borders.Bottom = C1.C1Preview.LineDef.Default

rt.Style.BackColor = Color.FromArgb(200, 210, 220)

rt.DataBinding.DataSource = ra.DataBinding.DataSource

ra.Children.Add(rt)

 

' This aggregate is also calculated over the group, but is connected to the data binding of the nested object:

agg = New C1.C1Preview.DataBinding.Aggregate("TotalNested_IntValue")

agg.ExpressionText = "Fields!IntValue.Value"

agg.Running = RunningEnum.All

agg.DataBinding = rt.DataBinding

doc.DataSchema.Aggregates.Add(agg)

 

' Add the area to the document:

doc.Body.Children.Add(ra)

      C#

// Create a Render area to be repeated for each group:

RenderArea ra = new RenderArea();

ra.Style.Borders.All = new LineDef("2mm", Color.Blue);

ra.DataBinding.DataSource = MyData.Generate(20, 0, true);

ra.DataBinding.Grouping.Expressions.Add("Fields!GroupId.Value");

 

// Make an aggregate that will calc the sum of IntValue fields within each group:

Aggregate agg = new Aggregate("Group_IntValue");

 

// Define the expression that will calc the sum:

agg.ExpressionText = "Fields!IntValue.Value";

 

// Specify that aggregate should have group scope:

agg.Running = RunningEnum.Group;

 

// Specify the data source for the aggregate:

agg.DataBinding = ra.DataBinding;

 

// Add the aggregate to the document:

doc.DataSchema.Aggregates.Add(agg);

 

// Make an aggregate that will calc the sum of IntValue fields over the whole document:

agg = new Aggregate("Total_IntValue");

 

// Define the expression to calc the sum:

agg.ExpressionText = "Fields!IntValue.Value";

 

// Specify that aggregate should have document scope:

agg.Running = RunningEnum.All;

 

// Specify the data source for the aggregate:

agg.DataBinding = ra.DataBinding;

 

// Add the aggregate to the document:

doc.DataSchema.Aggregates.Add(agg);

 

// Make the group header:

RenderText rt = new RenderText();

rt.Text = "GroupId: [Fields!GroupId.Value]";

rt.Style.BackColor = Color.Yellow;

ra.Children.Add(rt);

 

// This render text will print group records; as can be seen, group aggregate values can be referenced not only in group footer but also in group header and in group detail:

rt = new RenderText();

rt.Text = "GroupId: [Fields!GroupId.Value]\rIntValue:[Fields!IntValue.Value]\rGroup_IntValue:[Aggregates!Group_IntValue.Value]\rTotal_IntValue:[Aggregates!Total_IntValue.Value]\rTatalNested_IntValue:[Aggregates!TatalNested_IntValue.Value]";

rt.Style.Borders.Bottom = LineDef.Default;

rt.Style.BackColor = Color.FromArgb(200, 210, 220);

rt.DataBinding.DataSource = ra.DataBinding.DataSource;

ra.Children.Add(rt);

 

// This aggregate is also calculated over the group, but is connected to the data binding of the nested object:

agg = new Aggregate("TotalNested_IntValue");

agg.ExpressionText = "Fields!IntValue.Value";

agg.Running = RunningEnum.All;

agg.DataBinding = rt.DataBinding;

doc.DataSchema.Aggregates.Add(agg);

 

// Add the area to the document:

doc.Body.Children.Add(ra);

Note that there are also aggregate types that can be used in data-bound C1PrintDocuments without declaring them in the document's aggregates collection (Aggregates). For more details and an example, see the Data Aggregates topic.


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.