Mar 13, 2012

Images in Reports at Run-time


Query: How to put Images in Reports (Microsoft ReportViewer) through code at runtime/dynamically?
You can achieve this with charm. You can easily have images on your report, even the images that are on the Web.
Assumption: 
This article assumes that a user has basic idea about RDLC, Microsoft ReportViewer and DataSource. This Article does not teach the basics of how to generate Reports in Microsoft Report Viewer. 
To learn about ReportViewer and how to build reports, read this free book by Mahesh Chand:Reports using Report Viewer in Visual Studio 2005.

In ReportViewer, you have 2 options for images: External or Embedded.

1. 
Embedded: 
If you have images in your application and you want to use them.

Stage I: Then, you first need to embed the images in to be used in Reports. To embed an image in Reports:
a) Open the RDLC File.
b) Go to the Menu Item: 
Report > Embedded Images. Click on: New Image.
c) Browse for the image. Edit 
Name for the image chosen as desired (example, here: TestImage). OK.

Stage II:
d) Use an Image control on your RDLC file.
e) Set the 
Image's Property-Source = Embedded.
f) Reserve a Report's DataSource Member for the image Path. For example- DataSource.ImgPath.
g) Set the value for image path from the code in 
DataSource.ImgPath. 
    For example: DataSource.ImgPath = "TestImage";
h) Do the normal stuff to bind DataSource etc.
i) Done. Enjoy your image in the Report.



2. 
External: 
If you do not have images in your Application. That is, you want to use an external image.

a. Use an Image control on your RDLC file.
b. Set the 
Image's Property-Source = External.
c. Reserve a Report's DataSource Member for the image Path. For example- DataSource.ImgPath.
d. Set the value for image path from the code in 
DataSource.ImgPath.
    For example: DataSource.ImgPath = "http://www.google.com/intl/en_ALL/images/logo.gif";
e. The following self explanatory code also needs to be written:    reportViewer1.LocalReport.EnableExternalImages = true;  (reportViewer1 is the Name for the   ReportViewer Control)
f. Do the normal stuff to bind DataSource etc.
g. Done. Enjoy your image in the Report.

No comments:

Post a Comment

Hi,

Thanks for your visit to this blog.
We would be happy with your Queries/Suggestions.