July 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Reporting - Combining ReportViewer and MS Charts

  • May 20, 2009
  • By Matt Goebel and Rachel Baker
  • Send Email »
  • More Articles »

When you are finished your report should look similar to the following in design mode.



Click here for larger image

Figure 1.2 The finished report in design view

We will now add a new form ("Form2") to the project, and change the form's text to "Sales Report." This form is going to be used to display the report we just created. From the Toolbox, drag and drop the Microsoft ReportViewer control, found under the Reporting section. Once placed on the form, the control will ask you to specify the report (.rdlc) to use. From the dropdown, select reportSales.rdlc. This will automatically create your binding sources.

The next steps will be to capture the chart image, wire up the forms to pass the data, and bind that data to the report.

On Form1, add a new button and create the click event for the button. In the click event, add the following code to capture the image and store it in the ReportData object.

          private void cmdCreateReport_Click(object sender, EventArgs e)
          {
              using (MemoryStream ms = new MemoryStream())
              {
                  chartSales.SaveImage(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  
                  currentReportData.SalesChart = ms.ToArray();
              }
  
              Form2 fm2 = new Form2(currentReportData);
              fm2.ShowDialog(this);
          }

You will also notice in the code above that we are passing the ReportData object in the constructor to the new form. To do so, be sure to change Form2's constructor to accept the object as follows:

         private ReportData currentReportData;
  
          public Form2(ReportData reportData)
          {
              InitializeComponent();
  
              currentReportData = reportData;
          }
  The final step is to create the Load event for Form2 and set the report's data sources.  
          private void Form2_Load(object sender, EventArgs e)
          {
              ReportDataBindingSource.DataSource = currentReportData;
              SalespersonDataBindingSource.DataSource = currentReportData.SalesData;
  
              this.reportViewer1.RefreshReport();
          }

Build and run the application. When you click the button on Form1 to generate the report you should see the following, or similar depending on your formatting of the report.



Click here for larger image

Figure 1.3 The finished report as viewed from the application

Conclusion

The Microsoft Chart Controls and Reporting Services offer robust, flexible support for displaying information in your applications. We have shown how to create a simple chart and report. Additionally, we have demonstrated how you can reuse a chart from the application in a report to minimize development effort and for maintainability purposes.

About the Authors

Matt Goebel is a manager with Crowe Horwath LLP in the Indianapolis, Indiana, office. He can be reached at 317.208.2555 or matt.goebel@crowehorwath.com.

Rachel Baker is a senior developer with Crowe Horwath LLP in the Oak Brook, Illinois, office. She can be reached at 630.990.4434 or rachel.baker@crowehorwath.com.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel