FlexReport for WinForms | ComponentOne
In This Topic
    Grouping Data
    In This Topic

    Grouping is the most commonly used method to represent data in an organized manner. After designing the basic layout, you may decide to segregate the records by certain fields, or other criteria that would make the report easier to read. By grouping data, you can separate groups of records and display introductory and summary data for each group. The group break is based on a grouping expression. This expression is usually based on one or more recordset fields but it can be as complex as you want.

    In FlexReport, grouping is achieved by using C1FlexReport.Groups.

    Lets say you want to view a list of employees falling under a designation or title. In this case, the list should be grouped by Title. The following steps illustrate how to Group the list of employees by the Title. This example uses sample created in FlexReport Quick Start.

    1. Add a C1CheckBox to the Form in the FlexReport Quick Start project.
    2. Set the C1CheckBox Name to 'groupC1CheckBox' and Text to 'Group Report by Title'.
    3. Create CheckedChanged event as c1CheckBox1_CheckedChanged.
    4. Add the following code.
      Private grp As Group
      Private s As Section
      Private Sub c1CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
          If groupC1CheckBox.Checked Then
              ' group employees by title and sort titles in ascending order         
              grp = C1FlexReport1.Groups.Add("GrpTitle", "Title", SortEnum.Ascending)
              ' format the Header section for the new group           
              s = grp.SectionHeader
              s.Height = 1000
              s.Visible = True
              Dim f As New TextField()
              f.Name = "Title"
              f.Text.Expression = "Title"
              f.Left = 0
              f.Top = 0
              f.Width = C1FlexReport1.Layout.Width
              f.Height = 500
              f.Align = FieldAlignEnum.LeftMiddle
              f.Font.Bold = True
              f.Font.Size = 12
              f.Border = New Border(2, Color.Black, DashStyle.Solid)
              f.BackColor = Color.FromArgb(150, 150, 220)
              f.MarginLeft = 100
              s.Fields.Add(f)
              C1FlexReport1.Render()
          Else
                btnEmployees.PerformClick()
          End If
      End Sub
      
      Group grp;
      Section s;
      private void c1CheckBox1_CheckedChanged(object sender, EventArgs e)
              {
                  if (groupC1CheckBox.Checked)
                  {
                   // group employees by title and sort titles in ascending order         
                      grp = c1FlexReport1.Groups.Add("GrpTitle", "Title", SortEnum.Ascending);
                   // format the Header section for the new group           
                      s = grp.SectionHeader;
                      s.Height = 1000;
                      s.Visible = true;
                     
                      TextField f = new TextField();
                      f.Name = "Title";
                      f.Text.Expression = "Title";
                      f.Left = 0;
                      f.Top = 0;
                      f.Width = c1FlexReport1.Layout.Width;
                      f.Height = 500;
                      f.Align = FieldAlignEnum.LeftMiddle;
                      f.Font.Bold = true;
                      f.Font.Size = 12;
                      f.Border = new Border(2, Color.Black, DashStyle.Solid);
                      f.BackColor = Color.FromArgb(150, 150, 220);
                      f.MarginLeft = 100;
                      s.Fields.Add(f);
                      c1FlexReport1.Render();                                
                  }
                  else
                  {
                      btnEmployees.PerformClick();
                  }
              }
      
    5. Run the project. Click Employees button to render the report.
    6. Click 'Group Report by Title' checkbox to view grouping the report. Observe that the titles are sorted in the ascending order.
      Grouping Data in FlexReport