user5843610
user5843610

Reputation:

Create Telerik Reporting list object

Just started out using Telerik Report Designer and like to create a PDF using the details section bound to a list object. I created a people list object and add name and phone extension.

I dont see how to bound the list to the detail section and call the design file.

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

Upvotes: 0

Views: 985

Answers (1)

user1659955
user1659955

Reputation: 141

The detail section cannot be bound to data itself - it uses the data from the datasource of the report. The code above will not generate a report, because it just binds the data in the list to the report parameter, but not to the report itself. The code also uses TypeReportSource, but doesn't set its TypeName, hence the reportProcessor instance would likely throw an exception. I don't have Telerik Reporting installed to provide a tested solution, but basically you need to do this:

  • Add two textboxes to the report's detail section and set their expression to "=Fields.Id" and "=Fields.Name" (assuming the Person class has these properties).
  • The list of Person instances must be assigned as report datasource. Since you're doing it programmatically, you need to use InstanceDataSource and the code should look like this:

    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);
    
  • Save the result.DocumentBytes to some path of your choice. The approach is shown in Reporting's documentation in How to: Bind to a BusinessObject, In that example the report source is passed to the viewer, instead to the reportProcessor, but the idea is the same.

Upvotes: 1

Related Questions