zoukankan      html  css  js  c++  java
  • FastReport.Net中使用列表和数组作为报表数据源

    大多数现代报告工具允许您使用几乎任何数据库,然而,并不是所有报表工具都能以一个数据源的列表或数组来工作。本文中将展示如何使用FastReport .Net报表工具来实现。

    请注意以下重要几点:

    • 清单中的对象字段应该被描述为在报表中可见的公共属性;

    • 除列表外你可以从任何IEnumerable集中提交你的报表数据;

    • 在报表中转移LINQ查询结果你需要使用ToArray方法转换列表。

    创建窗体应用程序。
    在报表中给出类别列表,对于每个类别将增加产品列表。
    声明变量:

    private List<category> FBusinessObject;
    private Report FReport;</category>
    

    顾名思义,它是一个类别列表和报表对象。
    所以,添加一个类的产品:

    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;
     }
     }

    正如你所见,给对象的字段声明是公共的。
    现在添加类的类别:

    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>();
     }
     }</product></product></product>

    对象类别的字段之一是一个产品列表,也就是说,一个类别的列表是队列的一个数组。
    创建数据源:

    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);
     }</category>

    从注释中显而易见,创建了一个对象类别列表。然后创建一个新的类别,并将所需数量的产品添加进去,在类别列表中添加类别。再添加上几类产品。
    数据源已经创建,现在你需要在RegisterData方法的帮助下注册报表:

    public void RegisterData()
     {
     FReport.RegisterData(FBusinessObject, "Categories");
     }
    

    此方法显示报表中列表名为“Categories”。
    在设计器重添加方法运行报表:

    public void DesignReport()
     {
     FReport = new Report();
     CreateDataSource();
     RegisterData();
     FReport.Design();
     }
    

    在这里,我们创建了一个报表对象和数据源的实例。我们还注册了数据源,并在设计器中打开报表。
    添加一个按钮方法来调用报表设计器:

    public void button1_Click(object sender, EventArgs e)
     {
     DesignReport();
     }

    在报表设计器中,需要在菜单数据中选择数据源→选择报表数据…

    clipboard.png

    创建一个简单的Master-Detail类型报表:

    clipboard.png

    在预览模式下运行报表:

    clipboard.png

    综上所诉:FastReport .NET再次证明是一个灵活的,现代化产品,您可以在您的应用程序中使用必要的数据而不必将它们转换成数据表。

  • 相关阅读:
    解析Javascript事件冒泡机制
    LeetCode——Flatten Binary Tree to Linked List
    流动python
    HDU2586
    Cannot find ActionMappings or ActionFormBeans collection
    reactor设计模式
    简单的Ajax应用实例
    CString——Left、Right、Find、ReverseFind
    MATLAB新手教程
    八大排序算法总结
  • 原文地址:https://www.cnblogs.com/twodog/p/12139758.html
Copyright © 2011-2022 走看看