logo
small logo
  • Produkte
  • Shop
  • Support
  • Über uns
  • Customer panel Support
    • en
    • de
    • JP
    • ZH
  • Home
  • /
  • Articles
  • /
  • How to create a calculated field in a matrix from a report script FastReport .Net
  • FastReport.NET packages and .NET 5.0

    17. Dezember 2020

    UPD: Gilt für die Versionen von FastReport .NET vor 2022.2. Lizenzpakete sind jetzt auf unserem

    read more
  • Wie ein WebReport-Objekt in FastReport.Core aktualisieren

    21. September 2020

    Manchmal gibt es eine Aufgabe, den Report zu aktualisieren, z. B. wenn Sie einen neuen

    read more
  • Die Events PreviewControl.OnPrint und PreviewControl.OnExport

    1. Oktober 2020

    FastReport 2019.4 bietet eine Möglichkeit, die Events PreviewControl.OnPrint und PreviewControl.OnExport zu steuern, die direkt aufgerufen

    read more
  • MS Word versus Open Document Text. Was ist besser?

    19. Oktober 2020

    Wenn wir von Texteditoren sprechen, stellen wir uns die Leuchte sofort als Microsoft Word vor,

    read more
  • Wollten Sie Tabellen für den Benutzer generieren - Excel oder OpenOffice Calc?

    8. Oktober 2020

    Tabellen. Seit Jahrhunderten werden sie verwendet, um dieselbe Art von Daten für Buchhaltungszwecke, Zählbeträge und

    read more

How to create a calculated field in a matrix from a report script FastReport .Net

7. Oktober 2018

Matrix reports are very popular. Typically, the matrix, or otherwise summary tables are used in the accounting and statistical reporting. Let's determine what a summary table is.

A summary table is a software tool that allows you to reorganize and summarize the selected columns and rows of data in a table to obtain the required report. The summary table is especially useful for a large amount of data.

For example, you are running a small business like stationery store. You would like to know what products have the best selling rate, or what are the sales leaders this month. Having a lot of statistical data is quite laborious to analyze them. Here on the help comes a summary table, which combines data for a certain period in one table.

The main operation in the processing of data for creating a summary table is the summation. But, sometimes, you may need your own function to calculate some columns, such as totals. Let's have a look at the example of the way to add a calculated field to a matrix using the report script. Create a report and add the matrix object to the "Data" band. As you remember, to create a column, a drain or a cell, you need to drag the expression to the corresponding area. The expression can be: data field, function, variable, total. In general, any object from the data panel. If you are going to fill the matrix from the report's script, then add any expression to the matrix, and then open it for editing and clean it, or enter a number. This is the template of the matrix:

 

To add fields to the matrix programmatically, you need to create an AfterData event handler for the matrix.

 

Let's move on to the report script. In the generated handler, add the columns to the matrix:

1
2
3
4
5
6
7
8
9
 Matrix1.AddValue(new Object[] { "Accruals", "Salary" }, new Object[] { "1" }, new Object[] { 1000});
 Matrix1.AddValue(new Object[] { "Accruals", "Bonus" }, new Object[] { "1" }, new Object[] { 500});
 Matrix1.AddValue(new Object[] { "Accruals", "Penalty" }, new Object[] { "1" }, new Object[] { 200});
 Matrix1.AddValue(new Object[] { "Accruals", "Total" }, new Object[] { "1" }, new Object[] { 0});
 
 Matrix1.AddValue(new Object[] { "Accruals", "Salary" }, new Object[] { "2" }, new Object[] { 500});
 Matrix1.AddValue(new Object[] { "Accruals", "Bonus" }, new Object[] { "2" }, new Object[] { 300});
 Matrix1.AddValue(new Object[] { "Accruals", " Penalty" }, new Object[] { "2" }, new Object[] { 250});
 Matrix1.AddValue(new Object[] { "Accruals", "Total" }, new Object[] { "2" }, new Object[] { 0});

 Note that the structure of column headers is given by enumeration of names - "Charges", "Salary". In our case, the general header "Charges" has 4 subheadings (). Next, we create a row with heading 1. The third parameter is the value of the data cell new Object [] {1000}.

We added two rows of data. The Total field has a value of zero. Now we need to set a value for it. We pass through all rows of the matrix in a loop and set the value for the result:

1
2
3
4
5
6
int[] rowIndices = Matrix1.Data.Rows.GetTerminalIndices();
 for (int i = 0; i < rowIndices.Length; i++)
 {
 rowIndex = rowIndices[i];
 SetValue("Accruals;Total", GetValue("Accruals;Salary") + GetValue("Accruals;Bonus") - GetValue("Accruals;Penalty"));
 }

 Add the rowindex variable to the class:

private int rowIndex;

Note the SetValue and GetValue methods. Add them to the class:

1
2
3
4
5
6
7
8
9
10
11
private void SetValue(string complexValue, float value)
 {
 int columnIndex = Matrix1.Data.Columns.Find(complexValue.Split(';'));
 Matrix1.Data.SetValue(columnIndex, rowIndex, value);
 }
 
private float GetValue(string complexValue)
 {
 int columnIndex = Matrix1.Data.Columns.Find(complexValue.Split(';'));
 return new Variant(Matrix1.Data.GetValue(columnIndex, rowIndex, 0));
 }

 From the headings it is clear that one method is to get the value of a cell, and the second is to change.

Run the report:

 

So we calculated the total for each row of data.

about product buy
avatar
Dmitriy Fedyashov
Technical Writer
Fast Reports Team: Dmitriy Fedyashov - Technical Writer at Fast Reports
.NET FastReport

Add comment
logo
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314
  • Shop
  • Download
  • Dokumentation
  • Referenzen
  • Informationen zur Deinstallation unserer Produkte
  • FAQ
  • Tutorial Video
  • Forum
  • Support SLA
  • Articles
  • Unsere Nachrichten
  • Presse über uns
  • Partner
  • Außergewöhnliche Lizenzierung
  • Kontakte

© 1998-2023 by Fast Reports Inc.

  • Datenschutz

Trustpilot