This is a migrated thread and some comments may be shown as answers.

Bind Enumerable To Report Datasource

2 Answers 378 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Veteran
Jeff asked on 11 Oct 2020, 09:35 PM

I'm working in a ASP.net core 3.1 web application. I want to display a Report without using the report viewer as well as not embedding the datasource into the report. The data for the report will be determined at runtime. So this is what I have done so far.

Defined a report with the report designer that has just one field                    = Fields.Title

The report does render with the correct number of lines that I expected. However there is no data.  The data object that gets assigned to the report datasource does have values.  Five values from the database.  It's just that the those values don't  bind to the ["Fields.Title] property of the report. All I see is 

Title
Title
Title
Title
Title

Here's the code

Controller Method that gets called from the GUI
 
 public FileContentResult GetTasksReports()
        {
            return File(_reportService.GetTaskReport(), "application/pdf");
        }
 
 
Service methods
 
 
public byte[] GetTaskReport()
{
          //get data from db. This returns data
           var tasks = _taskRepository.GetAllList();
 
            //Populate a List of Classes that will serve as the Reports datasource
            List<MyTasks> taskList = new List<MyTasks>();
            foreach (Task t in tasks)
            {
                MyTasks ts = new MyTasks()
                {
                    Title = t.Title
                };
                taskList.Add(ts);
            };          
 
            return getReport(taskList, "TasksNoEmbeddedDS.trdp");
        }
         
 private byte[] getReport(dynamic ds, string telerikReportName)
        {
            string reportName = "Reports\\Tasks\\" + telerikReportName;
 
            ReportPackager reportPackager = new ReportPackager();
            Telerik.Reporting.Report report = null;
            using (var sourceStream = System.IO.File.OpenRead(reportName))
            {
                 
                report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
 
                //assign the data to the Report Datasource
                report.DataSource = ds;
            }
 
            return RenderMyReport(report);
 }
 
        private byte[] RenderMyReport(Telerik.Reporting.Report report)
        {  
            System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
 
            InstanceReportSource instanceReportSource = new InstanceReportSource()
            {
                ReportDocument = report
            };
 
            var configuration = ConfigurationHelper.ResolveConfiguration(webHostEnvironment);
            ReportProcessor reportProcessor = new ReportProcessor(configuration);
            RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);
 
            return result.DocumentBytes;
        }
 
 
 public class MyTasks
  {  
  public string Title;
 }      

 

 

 

2 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
Veteran
answered on 12 Oct 2020, 11:16 AM

So I was able to get the data to display in Report when the Report's DataSource is set to a List<object> when the Detail section has textboxes.  However I would like to use a Table object in the report but can't figure out how to bind the List<>  in my code to the Report Table object. 

I've played around setting the Report's objectDataSource to the List, but must be something wrong, or just completely going in the wrong direction.  

0
Jeff
Top achievements
Rank 1
Veteran
answered on 12 Oct 2020, 04:30 PM

I figured this out. I just got a handle to the table in the Report and set the table.DataSource to the List<objects>

var table = report.Items.Find("table1", true)[0] as Telerik.Reporting.Table;
table.DataSource = taskList;

 

Tags
General Discussions
Asked by
Jeff
Top achievements
Rank 1
Veteran
Answers by
Jeff
Top achievements
Rank 1
Veteran
Share this question
or