logo
small logo
  • Products
  • Buy
  • Support
  • About
  • Customer panel Support
    • en
    • de
    • JP
    • ZH
  • Home
  • /
  • Articles
  • /
  • How to filter the built matrix
  • How to select the top values in a matrix

    April 22, 2021

    The article is relevant until version 2022.1. FastReport .NET has a great tool for displaying data

    read more
  • How to sort similar matrices through one-dimensional array on several pages in FastReport .NET

    September 22, 2021

    Let's say we have the task: to sort the matrix on the first page in

    read more
  • How to make an interactive report with toggle sorting

    June 2, 2021

    Occasionally, our users face the task of making an interactive report. It may be

    read more
  • Complex report with multiple matrices in FastReport.NET

    June 16, 2021

    Today we will look at a complex report with a band-oriented approach, which is usually

    read more
  • How to sort matrix by indicator

    June 30, 2021

    Sorting data is a very important analysis tool that allows you to quickly assess the

    read more

How to filter the built matrix

June 23, 2021

In FastReport, the Matrix object allows us to filter data. This is one of the most popular functionalities and many users like to use it. However, sometimes filtering the original data is not an option.

Let's take a look at the matrix below.

Source matrix

As you can see, these are sales statistics by employees for 5 years. There are no statistics for Steven Buchanan employee for 2011, 2012 and 2015. This means that Steven will disappear from the matrix if we filter the matrix by year and count out 2013 and 2014.

To complete the picture, you need to count all employees, even if they did not have sales for the reporting period. In this case, you will have to use one of the options:

1) to artificially enrich the raw data with zero values for each year in which the employee had no sales;
2) to filter the already built matrix by removing unnecessary columns.

In the report template:

Report template

Let’s add the BeforePrint event for the Year cell:

We add the BeforePrint event for the Year cell

With the following code:

private int index = 1;
 private List<int> removeColumns = new List<int>();
 
 private void Cell4_BeforePrint(object sender, EventArgs e)
 { 
 if (new List<int>(){2013, 2014}.Contains((int)Cell4.Value))
 {
 removeColumns.Add(index); 
 }
 index++; 
 }

Here we have set the indexes of the columns we want to delete. Now we will create a ModifyResult event handler for the matrix to edit the already built “matrix” object:

 private void Matrix1_ModifyResult(object sender, EventArgs e)
 {
 removeColumns.Reverse();
 foreach (int del in removeColumns)
 { 
 Matrix1.ResultTable.Columns.RemoveAt(del);
 }
 }

You need to delete from the end — from the largest index, because when deleting columns or rows, the index of all subsequent ones is shifted. Therefore, the list of indexes for deletion was built in reverse order using the Reverse () method. Next, we simply deleted the columns for the corresponding indices. Let's see what we have in the end:

Summary table

The columns for 2013 and 2014 disappeared from the matrix, but employee Steven Buchanan remained. We have the desired effect! You can also delete rows you don't need using another collection Matrix1.ResultTable.Rows. Now you know how to filter a matrix when data filtering is not an option.

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

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
  • Buy
  • Download
  • Documentation
  • Testimonials
  • How to uninstall
  • FAQ
  • Tutorial Video
  • Forum
  • Articles
  • Our News
  • Press about us
  • Resellers
  • Extended licensing
  • Contact us

© 1998-2023 by Fast Reports Inc.

  • Privacy Policy