Reputation: 107
I am new to using the report viewer control.
The way I am currently creating a report is by calling a stored procedure and inserting input parameters to query a table in a database and to return a list of rows.
The rows are inserted into a Data Table and the Data Table is returned.
The data table is binded to a gridview which the user can view.
Now my problem....
I would like to use the report viewer to display the data instead of the gridview so the users can easily export the data and view a nice graph with it.
The Stored Procedure is called from my data access class which is just a class I have in my project (it is not in a folder).
When I go to create a report viewer and get asked for my datasource my data access class does not show up. How can I get the results (the returned data table) from my stored procedure function to get inserted into the report viewer?
Upvotes: 1
Views: 2390
Reputation: 107
What I ended up doing to get the report viewer to access my datatable was add a dataset to my project. Within that dataset I had to create a datatable with the same column names as in my code generated data table. From there I went to my report and added a table and pointed it towards the datatable which was created within the dataset. Then in my code I programmatically added the datatable using the code described above from Shelby.
Upvotes: 0
Reputation: 2867
You could try the programmatic approach,
//Load Report Definition.
// From File.
ReportViewer1.LocalReport.ReportPath = "C:\\Report.rdlc";
//Load Report Data.
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", data));
//Refresh Control.
ReportViewer1.LocalReport.Refresh();
If you have a DataTable with your data in it, you simply Add it to the ReportViewer as a DataSource. "DataSet1" is the name of the dataset in the report currently loaded into the report viewer.
So if you were using Entity Framework it would be like this.
//Create Connection.
Entities db = new Entities();
//Get the Data Using the query supplied (Where Entities.SomeObject is the Entity to retrieve data).
IQueryable<Object> data = db.CreateQuery<Object>("SELECT VALUE c FROM Entities.SomeObject AS c WHERE c.SomeValue> 0");
//Reset Control. Doesn't Usually work if this is skipped for some reason.
ReportViewer1.Reset();
//Load Report Definition.
// From File.
ReportViewer1.LocalReport.ReportPath = "C:\\Report.rdlc";
//Load Report Data.
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", data));
//Refresh Control.
ReportViewer1.LocalReport.Refresh();
Same should work for a DataTable.
Upvotes: 1