[ACCEPTED]-How can I load datatable as ReportDataSource?-reportviewer
You are not initializing the ReportDataSouce 5 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 4 need to alter the first parameter to the 3 ReportDataSource constructor to set the 2 name of the datasource that your report 1 is expecting.
I believe that madisonw's answer above is 17 correct, but Luke's comment about using 16 the DataSet name as named in the .rdlc report file, for the Datasource.Name 15 property, perhaps needs to be emphasized. For 14 me this was the main bugaboo that kept my 13 app from working. It can be found by opening 12 the .rdlc file as an XML file by using the 11 "Open with..." command. The default I think 10 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 9 made was not including the .rdlc file in 8 the Debug (or Release) folder. This was 7 fixed by right-clicking on the .rdlc file 6 in Solution Explorer, then Properties, then 5 setting "Copy to Output Directory" to "Copy 4 Always".
Once these two parts were corrected 3 my program worked to use ReportViewer within 2 a console app to generate a PDF file with 1 no interface.
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();
Imagine the DataSources block of your RDLC 5 file is like below:
< DataSets > < DataSet 4 Name="DataSet1_Lot" >
then the relative code 3 should be:
string name = "DataSet1_Lot"; // this must exist in the RDLC file DataTable 2 dt = new DataTable();
Microsoft.Reporting.WinForms.ReportDataSource 1 rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(name, dt);
More Related questions