创建Telerik报告列表对象

问题描述 投票:0回答:1

刚开始使用Telerik Report Designer,并且喜欢使用绑定到列表对象的详细信息部分创建PDF。我创建了一个人员列表对象,并添加了姓名和电话分机。

我不知道如何将列表绑定到详细信息部分并调用设计文件。

List<Person> people = new List<Person>();
            people.Add(new Person(501, "Joe"));
            people.Add(new Person(302, "Bill"));
            people.Add(new Person(263, "Tom"));
            people.Add(new Person(244, "Mark"));
            people.Add(new Person(567, "Jim"));
            people.Add(new Person(662, "Jen"));

            Telerik.Reporting.ReportParameter reportParameter1 = new Telerik.Reporting.ReportParameter();
            reportParameter1.AvailableValues.DataSource = people;
            var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
            var reportSource = new Telerik.Reporting.TypeReportSource();
            string documentName = "NCCN Telephone List";


            var deviceInfo = new System.Collections.Hashtable();

            deviceInfo["OutputFormat"] = "PDF";


            Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", reportSource, deviceInfo);

            string fileName = result.DocumentName + "." + result.Extension;
            string path = System.IO.Path.GetTempPath();
            string filePath = System.IO.Path.Combine(path, fileName);

            using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
            {
                fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
            }
c# telerik-reporting
1个回答
0
投票

详细信息部分不能绑定到数据本身-它使用报表数据源中的数据。上面的代码不会生成报告,因为它只是将列表中的数据绑定到report参数,而不绑定到报告本身。该代码还使用TypeReportSource,但未设置其TypeName,因此reportProcessor实例可能会引发异常。我没有安装Telerik Reporting来提供经过测试的解决方案,但是基本上您需要这样做:

  • [将两个文本框添加到报告的详细信息部分并设置其表达式为“ = Fields.Id”和“ = Fields.Name”(假设Person类具有这些属性)。
  • Person实例列表必须是分配为报告数据源。既然你在做以编程方式,您需要使用InstanceDataSource并且代码应如下所示:

    var report = new MyReport();
    report.DataSource = people;
    var irs = new InstanceReportSource(){ ReportDocument = report };
    var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
    var result = reportProcessor.RenderReport("PDF", irs, null);
    
  • 将结果.DocumentBytes保存到您选择的某个路径。该方法显示在How to: Bind to a BusinessObject中的Reporting文档中,在该示例中,报告源传递给查看器,而不是传递给reportProcessor,但是思想是相同的。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.