How can I load datatable as ReportDataSource?

2019-01-15 10:31发布

问题:

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' "

回答1:

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.



回答2:

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.



回答3:

this.reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.Reset();
reportViewer1.LocalReport.ReportEmbeddedResource = "Your Report Name.rdlc";
SqlConnection con = new SqlConnection();
con.ConnectionString = "Connection";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from YourTableName";
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
con.Close();     
ReportDataSource rprtDTSource= new ReportDataSource();
rprtDTSource.Name = "reportDataSetName";
rprtDTSource.Value = dt;
this.reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
this.reportViewer1.RefreshReport();


回答4:

Imagine the DataSources block of your RDLC file is like below:

< DataSets > < DataSet Name="DataSet1_Lot" >

then the relative code should be:

string name = "DataSet1_Lot"; // this must exist in the RDLC file DataTable dt = new DataTable();

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