The way to add a data field to a matrix row

2018-10-08

In this article, we'll have a look at the way to create a dynamic matrix that is populated from the report's script code. The peculiarity of this matrix is that in addition to the data added from the script, we will insert the data field in the report cell using a text object.

Suppose you create a matrix with a variable number of columns, which will be added depending on any conditions, but some of the data you have is constant. They are always filled. It would be nice just to put these data fields simply in the matrix, and the remaining cells should be filled from the code. Despite the fact that the filling of the matrix data is provided only in two ways (from the code or automatically, the data fields) we will match them.

In fact, the technology is very simple. The data field is simply inserted into the cell as a separate text object. However, this is not all. When you add data to the matrix in the report, you must add the row number from the data set.

Let's have a look at the example.

Create a report and add the matrix to the "Data" band. Connect the data source - the demonstration database from the delivery, the Employees table.

The matrix template looks like this:

In the cell with a value of 2, we added a text object. In it, select the field Employees.LastName. This is exactly the "static" field about which we have spoken in above.

Create the AfterData event handler for the matrix.

1
2
3
4
5
6
7
8
9
10
11
12
13
private void Matrix1_AfterData(object sender, EventArgs e)
 {
 DataSourceBase rowData = Report.GetDataSource("Employees"); // we get the data source Employees.
 Matrix1.DataSource = rowData; // assign it to the DataSource matrix property 
 
 rowData.Init(); // initialize the data source
 // we go through all records of the data source
 while (rowData.HasMoreRows)
 { 
 Matrix1.Data.AddValue(new Object[] {"Phone" }, new Object[] { (string)Report.GetColumnValue("Employees.City"), (string)Report.GetColumnValue("Employees.FirstName")}, new Object[] {(string)Report.GetColumnValue("Employees.HomePhone") }, Report.GetDataSource("Employees").CurrentRowNo); // add another record
 rowData.Next(); //get the next record
 }
 }

 It is clear from the comments that when you add a data string, we define the title of Phone. Then we insert the data in order: city, name and phone. And also, we need to transfer the data line number to insert the last name from the current data record.

Now run the report:

 

As you can see, you can make it easier for yourself and not add all the necessary data in the report script, but simply place them in the matrix template using a text object.

.NET FastReport Data Source
June 22, 2026

How to Configure a Report with Business Objects in Code and the FastReport .NET Designer

This article demonstrates a practical example of creating and using an .frx report template that connects to hierarchical Business Objects in FastReport .NET.
April 28, 2026

New Report Validation System in FastReport VCL

In this article, we'll explain how report validation works, how to set it up, how to write your own rules, and share some interesting new features.
April 21, 2026

Using Watermarks in FastReport VCL

The article provides a detailed overview of the watermark functionality in FastReport VCL — covering both the visual interface and programmatic methods using Delphi code and report scripts.

© 1998-2026 Fast Reports Inc.