How can I load a datatable as a 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 a datatable as a ReportDataSource?

The current code produces: "It is not possible to implicitly convert the type 'System.Data.DataTable' to 'Microsoft.Reporting.WinForms.ReportDataSource'"

+4
source share
4 answers

You are not initializing ReportDataSouce correctly. 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(); 

In addition, you may need to change the first parameter in the ReportDataSource constructor to set the name of the data source expected by your report.

+9
source

I believe the madisonw answer above is correct, but Luke commented on the use of the DataSet name named in the .rdlc report file for the Datasource.Name property, perhaps it should be emphasized. For me, this was a major bugaboo that prevented my application from working. You can find it by opening the .rdlc file as an XML file using the "Open With ..." command. By default, I think just "DataSet1":

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

Another mistake I made did not include the .rdlc file in the Debug (or Release) folder. This was fixed by right-clicking on the .rdlc file in Solution Explorer, then Properties, and then Copy to Output Directory to Copy Always.

After fixing these two parts, my program worked on using ReportViewer in a console application to create a PDF file without an interface.

+4
source
 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(); 
+2
source

Imagine the DataSources block of your RDLC file looks like this:

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

0
source

All Articles