In this article I am going to explain how to dynamically create a report by using report viewer in windows form. To view reports that exist on local file system, you can use the WinForms ReportViewer control to render them in a Windows application.
The following example demonstrates how to render a report using ReportViewer control.
To add the ReportViewer Control to a Windows application
· Create a new Windows application using Microsoft Visual C#.
· Locate the ReportViewer control in the Toolbox.
· Drag the ReportViewer control onto the design surface of the Windows Form. A ReportViewer control named reportViewer1 is added to the form.
This example also uses the following class (Student) and its properties and methods to display the data in report. Please create or include the following class and its content in your application.
To add the Student details report to a Windows application
· From the project menu, select Add New Item.
· Select Report and edit the name and click the Add button. The StudentReport.rdlc file should now be part of the project.
In Data Source Configuration wizard choose a Data Source Type as Object.
In the next step, select Student class and click on Finish button.
· Add a new dataset in StudentReport.rdlc page and name the dataset as StudentDS.
· Choose Student from the Available dataset options.
· Click on OK button to finish the step.
Insert a Table in the rdlc page as shown in the figure below:
Drag and drop the dataset fields into the inserted table columns.
To display the student data in the format of chart choose Insert -> Chart from the context menu as shown below:
Select chart type from variety of options given in the Select Chart Type dialog. Here I am choosing default chart type.
After adding a chart, do the following steps to display the student data in chart format:
· Drag the Sum field from the dataset and drop it into the data field of Chart.
· Drag the Name field from the dataset and drop it into the Category field of Chart.
· Edit the X axis title to Name and Y axis title to Marks in order to make both the axis meaningful.
Now bind StudentReport.rdlc with the report viewer as shown below:
The following code example will render the Student report in report viewer.
Now run or debug this application to see the output:
Thanks for reading this article. You can enter your valuable
comments and suggestion to improve this article in the comment box.
In order to get the source code of this application, you can enter your valuable comment.