• 使用ActiveReport for .net 进行报表开发(三)显示对象集合中的数据


    上篇随笔演示了在ActiveReport for .net中如何绑定数据源,例如DataSetDataView。本文将通过一些示例代码继续演示如何绑定对象集合以及如何从对象集合或列表中提取数据。

    1.       直接绑定:

    我们可以自己定义一个Collection,从IList继承,并使其中的每个对象都是一个实体,例如:

    CustomerCustomerCollection。然后给ActiveReport报表的DataSource直接赋值为CustomerCollection的实例就可以了。这种绑定方法在ActiveReport for .net自带的例子里有完整的演示。

    2.       手工加载显示对象的数据:

    在这里要用到两个事件:DataInitializeFetchData

    DataInitialize用来定义字段列表,获取数据等工作,FetchData用来指定每个字段的值。

    我们简单定义一个Customer

    public class Customer

            {

                    public int ID;

                    public string Name;

                    public string Address;

        }

    再给报表定义一个Customer数组,用来保存要显示的数据,比如:

    public Customer[] customers;

    还需要一个值来保存当前记录的索引:private int index =0;

    接下来在DataInitialize中定义报表有那些字段:

    this.Fields.Add("Name");

        this.Fields.Add("Address");

    然后在FetchData事件中添加以下代码:

    if (this.index >= this.customers.Length )

            {

                    eArgs.EOF = true;

                    return;

            }

            else

            {

                    eArgs.EOF = false;

            }

            this.Fields["Name"].Value = customers[this.index].Name;

            this.Fields["Address"].Value = customers[this.index].Address;

        this.index += 1;

    这段代码的含义就是将Customer数组中的每个Customer实例的NameAddress属性值赋给报表的NameAddress字段

    代码中的第一个if分支是判断是否已经加载到了最后一条数据,如果是,设置eArgs.EOFture,并返回,否则继续加载下一条数据。

    然后我们可以将要显示的数据放在Customers数组中,并启动显示报表:

    Customer[] customers = new Customer[2];

            Customer c = new Customer();

            c.Name = "James";

            c.Address = "Noljadfallsjf";

            Customer c2 = new Customer();

            c2.Name = "Joe";

            c2.Address = "adfaafadf";

            customers[0] = c;

            customers[1] = c2;

                           

            rpt.customers = customers;

            rpt.Run();

        this.viewer1.Document = rpt.Document;

     

    通过上面的示例可以看到,第二种方式虽然比较烦琐,需要程序员自己处理很多东西,但是这样程序员拥有完全的操控能力,特别是需要对取出的数据作很多复杂的处理时,这样的操控能力是非常重要的。

     

    在下一篇随笔中,将展示如何使用子报表来作显示主从表。

  • 相关阅读:
    测试平台系列(93) 免费的gitee当oss真的香吗
    C/C++ 结构体内存分布
    c# 委托与事件学习
    unity随记
    unity 对于对象刚体不断抖动的处理
    uniapp .keystore文件生成后不可食用的解决方案
    照片尺寸
    Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.
    ConstraintLayout
    Navigation库支持的参数类型
  • 原文地址:https://www.cnblogs.com/dahuzizyd/p/Active_Report_Net_3.html
Copyright © 2020-2023  润新知