Reports for WinForms | ComponentOne
Working with C1PrintDocument / Data Binding / Data Binding Examples / Using Aggregates
In This Topic
    Using Aggregates
    In This Topic

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

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' 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)
    

    To write code in C#

    C#
    Copy Code
    // 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 C1PrintDocument without declaring them in the document's aggregates collection (Aggregates). For more details and an example, see the Data Aggregates topic.