Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load datatable as ReportDataSource?

I am trying to do something like:

this.reportViewer.LocalReport.DataSources.Clear();
DataTable dt = new DataTable();
dt = this.inputValuesTableAdapter.GetData();    
Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource();

rprtDTSource = dt; // this line generates exception   

//this.reportViewer.LocalReport.DataSources.Add(rprtDTSource);
this.reportViewer.RefreshReport();

How can I load datatable as ReportDataSource?

The current code produces: "Cannot implicitly convert type 'System.Data.DataTable' to 'Microsoft.Reporting.WinForms.ReportDataSource' "

like image 334
ehmad Avatar asked Dec 06 '22 01:12

ehmad


2 Answers

You are not initializing the ReportDataSouce correctly. Give this a try:

this.reportViewer.LocalReport.DataSources.Clear(); 
DataTable dt = new DataTable(); 
dt = this.inputValuesTableAdapter.GetData();     

Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt); 

this.reportViewer.LocalReport.DataSources.Add(rprtDTSource); 
this.reportViewer.RefreshReport(); 

Also, you might need to alter the first parameter to the ReportDataSource constructor to set the name of the datasource that your report is expecting.

like image 86
madisonw Avatar answered Dec 15 '22 12:12

madisonw


I believe that madisonw's answer above is correct, but Luke's comment about using the DataSet name as named in the .rdlc report file, for the Datasource.Name property, perhaps needs to be emphasized. For me this was the main bugaboo that kept my app from working. It can be found by opening the .rdlc file as an XML file by using the "Open with..." command. The default I think is simply "DataSet1":

<DataSets>
  <DataSet Name="DataSet1">
    <Fields>
      <Field Name="BusinessEntityID">
        <DataField>BusinessEntityID</DataField>
        <rd:TypeName>System.Int32</rd:TypeName>
      </Field>

One other mistake I made was not including the .rdlc file in the Debug (or Release) folder. This was fixed by right-clicking on the .rdlc file in Solution Explorer, then Properties, then setting "Copy to Output Directory" to "Copy Always".

Once these two parts were corrected my program worked to use ReportViewer within a console app to generate a PDF file with no interface.

like image 37
MQuiggGeorgia Avatar answered Dec 15 '22 13:12

MQuiggGeorgia