how to set Datatable as datasource in ReportViewer
Asked Answered
S

3

3

I was searching in the last question about Datatable as datasource in ReportViewer and i found this as solution

DataTable table = new DataTable();
table.Columns.Add("value", typeof(string));
table.Columns.Add("price", typeof(string));
table.Columns.Add("quantity", typeof(string));

table.Rows.Add("test1","10","20");
table.Rows.Add("test2", "10", "20");

reportViewer1.LocalReport.DataSources.Clear();

ReportDataSource rprtDTSource = new ReportDataSource("TITLE",table);

reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
reportViewer1.RefreshReport();

but i get this image as result

enter image description here

what is the problem ??

Scarlatti answered 24/12, 2015 at 22:31 Comment(2)
Where do you set the report for your report viewer? I suppose you have a report as embedded resource and then you can set the report source something like this: this.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.ReportName.rdlc";Inglis
i just have and data table and report viewer, how i can display the data inside the report viewer @RezaAghaeiImparipinnate
I
5

It seems you have forgotten to set the report source for your report viewer control. You can set the report source using either of this options:

For example, I suppose you have added a report to your project, so you can show it in the report viewer this way:

var reportDataSource1 = new ReportDataSource("NameOfReportDataSet", YourDataTable);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.ReportName.rdlc";
this.reportViewer1.RefreshReport();

Also you can simply set the report of the report viewer using designer. Put a report viewer on your form and click on top-right arrow to open the smart tag window of report viewer, then choose a report from combo box.

enter image description here

Inglis answered 24/12, 2015 at 22:59 Comment(10)
i just have and data table and report viewer, how i can display the data inside the report viewer, can you give me an example?? please !Imparipinnate
yes, i did this action add a new empty report to project and choose it as resource for report viewerImparipinnate
but how i can pass the datatable items to report ??Imparipinnate
Creating an empty report is useless. You should add a data source to your report and design the report. Then pass your data to report. You can use the report wizard.Inglis
OK, i have create new report from database (table) the report display the data no problem, but how i can pass "DataTable table " items to report ??Imparipinnate
Good job, how did you show data of database in your report. You can show data of a custom data table the same way. Also you should pay attention, your custom data table schema should be like the table you used for creating report.Inglis
Also you should know if you have for example ProductDataTable you don't need to create a custom table and pass it to your report, you can use that table. Specially if you have created a data set that contains the data table and you are using a binding source as data source of report, so you can simply add some row to the the instance of yourSataSet.YourTable.Rows and then RefreshReportInglis
Let us continue this discussion in chat.Imparipinnate
By the way, just to mention if you don't know, When you accept an answer, you can also vote for it. It's not compulsory at all but its reasonable and recommended. If you know or if you voted, just ignore the comment :)Inglis
@RodrigoRodriguez You're welcome, happy to hear it was helpful for you :)Inglis
U
0

You can add source like below

LocalReport report = new LocalReport();

string startupPath = Environment.CurrentDirectory;
report.ReportPath = startupPath + @"\RDCLTemplate.rdlc";
report.Refresh();
Ultramicrochemistry answered 24/12, 2015 at 22:36 Comment(0)
D
0

If i am not wrong, ReportDataSource ctor you are using needs data source in first parameter i.e. a named data source. You're not supplying this, you need the DataTable name.

Update your code to this:

DataTable dt = new DataTable();
dt.TableName = "myDataTable";
//Fill Datatable
ReportDataSource source = new ReportDataSource("myDataTable", dt);
Dunfermline answered 24/12, 2015 at 22:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.