How to use lists and arrays as a data source for the report

2016-04-18

Most of the  modern reporting tools allow you to use almost any database. However, not all of them can work with lists or arrays of objects as a data source. In this article I'll show how to do it on the example of FastReport .Net reporting tool.

I will mention some important points:

• object fields from the list should be described as a public property to be visible in the report;

• in addition to the lists you can send in your report data from any iEnumerable collections;

• to transfer linq query results in the report you need to convert list using ToArray method.

Create a Windows Forms application.
Give a list of categories in the report. For each category there will be added the list of products.
Declare variables:

1
2
private List<Category> FBusinessObject;
private Report FReport;

As the name suggests, it is a list of categories and report object.

So, add a class of products:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Product
 {
 private string FName;
 private decimal FUnitPrice;
 
 public string Name
 {
 get { return FName; }
 }
 
 public decimal UnitPrice
 {
 get { return FUnitPrice; }
 }
 
 public Product(string name, decimal unitPrice)
 {
 FName = name;
 FUnitPrice = unitPrice;
 }
 }

As you can see, the fields of the object declared as public.
Now add the class of categories:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Category
 {
 private string FName;
 private string FDescription;
 private List<Product> FProducts;
 
 public string Name
 {
 get { return FName; }
 }
 
 public string Description
 {
 get { return FDescription; }
 }
 
 public List<Product> Products
 {
 get { return FProducts; }
 }
 
 public Category(string name, string description)
 {
 FName = name;
 FDescription = description;
 FProducts = new List<Product>();
 }
 }

One of the fields of the object Category is a list of products. That is, a list of categories is an array of arrays.

Let's create a data source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void CreateDataSource()
 {
 FBusinessObject = new List<Category>(); //Create list of categories
 
 Category category = new Category("Beverages", "Soft drinks, coffees, teas, beers"); //Create new instance of category
 category.Products.Add(new Product("Chai", 18m)); //Add new product to category
 category.Products.Add(new Product("Chang", 19m));
 category.Products.Add(new Product("Ipoh coffee", 46m));
 
 FBusinessObject.Add(category); //Add the category to the List
 
 category = new Category("Confections", "Desserts, candies, and sweet breads");
 category.Products.Add(new Product("Chocolade", 12.75m));
 category.Products.Add(new Product("Scottish Longbreads", 12.5m));
 category.Products.Add(new Product("Tarte au sucre", 49.3m));
 
 FBusinessObject.Add(category);
 
 category = new Category("Seafood", "Seaweed and fish");
 category.Products.Add(new Product("Boston Crab Meat", 18.4m));
 category.Products.Add(new Product("Red caviar", 15m));
 
 FBusinessObject.Add(category);
 }

From the comments it is clear that a list of objects Category is created. Then create a new category and add the desired number of products to it. Then add the category in the categories list. So we add a few categories with products.

We have created a data source. Now you need to register the report with the help of RegisterData method:

1
2
3
4
public void RegisterData()
 {
 FReport.RegisterData(FBusinessObject, "Categories");
 }

This method will make your list available in the report with the name: "Categories".

Add a method of running the report in the designer:

1
2
3
4
5
6
7
public void DesignReport()
 {
 FReport = new Report();
 CreateDataSource();
 RegisterData();
 FReport.Design();
 }

Here we have created an instance of the report object and data source. Also we registered data source and opened the report in the designer.

Add a button of the method of calling the report designer:

1
2
3
4
 public void button1_Click(object sender, EventArgs e)
 {
 DesignReport();
 }

 In the report designer, you need to select a data source in the menu Data -> Choose Report Data ...

 

Create a simple report of the Master-Detail type:

 

And run the report in preview mode:

 

To sum up:  FastReport .Net once again proved being a flexible, modern product. You can use necessary data from your application without having to worry about converting them into DataTable.

November 20, 2024

Localization and Language Switching in FastReport VCL

FastReport VCL supports 40 languages for interface localization and allows you to change the language on the fly through menus or code, without recompilation.
November 01, 2024

New Features of the FastReport VCL Editor

We are considering new features of the report editor: extension lines, highlighting of intersecting objects, updated report and data trees.
October 30, 2024

Using Styles When Creating Reports in FastReport VCL

The article discusses one of the new features of FastReport VCL — the use of styles and style sheets.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.