Reports for WinForms | ComponentOne
Working with C1PrintDocument / Data Binding / Data Binding Examples / Working With Groups
In This Topic
    Working With Groups
    In This Topic

    A typical use of grouping is demonstrated by the following code:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' A RenderArea is created that is 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)
     
    ' MyData array of objects is used as the data source:
    ra.DataBinding.DataSource = MyData.Generate(100, 0)
     
    ' Data is grouped by the GroupId field:
    ra.DataBinding.Grouping.Expressions.Add("Fields!GroupId.Value")
     
    ' Create a RenderText that will serve as the group header; In a general case, the header can be complex and itself be data bound:
    Dim rt As C1.C1Preview.RenderText = New C1.C1Preview.RenderText
     
    ' The group header will look like "GroupId = XXX", where XXX is the value of the GroupId field in the group:
    rt.Text = "GroupId: [Fields!GroupId.Value]"
    rt.Style.BackColor = Color.Yellow
     
    ' Add the header to the group area:
    ra.Children.Add(rt)
     
    ' This RenderText will print records within each group:
    rt = New C1.C1Preview.RenderText
     
    ' The text to print for each record:
    rt.Text = "GroupId: [Fields!GroupId.Value]" & Microsoft.VisualBasic.Chr(13) & "IntValue: [Fields!IntValue.Value]"
    rt.Style.Borders.Bottom = C1.C1Preview.LineDef.Default
    rt.Style.BackColor = Color.FromArgb(200, 210, 220)
     
    ' Set the text's data source to the data source of the containing RenderArea - this indicates that the render object is bound to the current group
    in the specified object:
    rt.DataBinding.DataSource = ra.DataBinding.DataSource
     
    ' Add the text to the area:
    ra.Children.Add(rt)
    

    To write code in C#

    C#
    Copy Code
    // A RenderArea is created that is to be repeated for each group.
    RenderArea ra = new RenderArea();
    ra.Style.Borders.All = new LineDef("2mm", Color.Blue);
     
    // MyData array of objects is used as the data source:
    ra.DataBinding.DataSource = MyData.Generate(100, 0);
     
    // Data is grouped by the GroupId field:
    ra.DataBinding.Grouping.Expressions.Add("Fields!GroupId.Value");
     
    // Create a RenderText that will serve as the group header; In ageneral case, the header can be complex and itself be data bound:
    RenderText rt = new RenderText();
     
    // The group header will look like "GroupId = XXX", where XXX is thevalue of the GroupId field in the group:
    rt.Text = "GroupId: [Fields!GroupId.Value]";
    rt.Style.BackColor = Color.Yellow;
     
    // Add the header to the group area:
    ra.Children.Add(rt);
     
    // This RenderText will print records within each group:
    rt = new RenderText();
     
    // The text to print for each record:
    rt.Text = "GroupId: [Fields!GroupId.Value]\rIntValue: [Fields!IntValue.Value]";
    rt.Style.Borders.Bottom = LineDef.Default;
    rt.Style.BackColor = Color.FromArgb(200, 210, 220);
     
    // Set the text's data source to the data source of the containing RenderArea - this indicates that the render object is bound to thecurrent group
    in the specified object:
    rt.DataBinding.DataSource = ra.DataBinding.DataSource;
     
    // Add the text to the area:
    ra.Children.Add(rt);