logo

Numeration of rows and columns in a matrix report

 You can learn how to create matrix reports here: https://kb.elma-bpm.com/article-3570.html

The need to add numbers to rows and/or columns in such a report arises often. You can do it using program code on the Code page (see fig. 1).

As an example, let's create a simple query in the data source of a report:

select FULLNAME Full name, month(EmployDate) Month, count(*) Number
from "User"
group by fullname, month(EmployDate)

Open the Report Layout tab and add a Matrix component to the Page Header line. To do so, in the left panel click on the Matrix object, then mouse over the Page Header line and click again. To learn more about the Report Layout tab, read Help.

   

 – adding a matrix to the report.

Move the Full Name, month and number fields to the matrix from the Data panel.

Fig. 1

Add code for the report on the Code page (Fig. 1):

using System;
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils;
 
namespace FastReport
{
  public class ReportScript
  {
    // method called in the Matrix object, in the AfterData event
    private void Matrix1_AfterData(object sender, EventArgs e)
    {
      // remove the AfterData method from the ResultTable object, if it had been assigned earlier
      Matrix1.ResultTable.AfterData -= Matrix1ResultTable_AfterData;
     // assign the AfterData method for the ResultTable object
      Matrix1.ResultTable.AfterData += Matrix1ResultTable_AfterData;
    }
     
    private void Matrix1ResultTable_AfterData(object sender, EventArgs e)
    {
     // get the sender results table
      TableResult table = sender as TableResult;
 
        
      // ------------------- new line with the column numeration ---------
      TableRow newRow = new TableRow();
     // add a row to the results table
      table.Rows.Insert(1, newRow);
       // go through all the cells of the new row
      for (int i = 0; i < table.ColumnCount; i++)
      {
        // set style for the new cells, using the style of the top left cell
        table[i, 1].SetStyle(table[0,0]);
        // set text (# - for the first column and numbers for the others)
        table[i, 1].Text = i == 0 ? "#" : i.ToString();
      }
         
      // ------------------- new column with numeration -------------------
      TableColumn newColumn = new TableColumn();
     // add a new column to the results table
      table.Columns.Insert(1, newColumn);
      // go through all the cells of the new column
      for (int i = 0; i < table.RowCount; i++)
      {
       // set style for the new cells, using the style of the top left cell
        table[1, i].SetStyle(table[0,0]);
        // set text for each cell except for the first one (this is the header, leave it empty)
        if (i > 1)
          table[1, i].Text = (i - 1).ToString();
      }
       
      // increase the number of fixed column by 1
      table.FixedColumns = 2;
      table.FixedRows = 2;
       
    }
 
  }
}
 

 After adding the code, go to the Matrix object in the report.

 

 For this, click on the  icon at the top left corner of the Matrix. The object properties will appear:

 

 In the properties, click to open the events of this object:

In the AfterData event select the Matrix_AfterData method. This way you will set the handler for the event after getting data in the Matrix object.

To check the result, you can click Debugging in the toolbar:

As the result, an additional row with numeration will be added.