Thursday, March 12, 2015

To change the image dynamically in crystal reports

If you are using Crystal Reports XI or later you can do this programatically using the 'Graphic Location' conditional formula.

To do this, perform the following steps:

1. Open the report in the Crystal Reports Designer.

2. Create a formula called "Location" that contains a temporary path string to an image file. For example, "C:\Images\Image1.jpg".

3. Right-click the image on the report and click 'Format Graphic'. The 'Format Editor' dialog box appears.

4. In the 'Picture' tab, click the 'Graphic Location' conditional formula button. The Format Formula Editor appears.

5. Call the "Location" formula from this conditional formula.

6. Save changes to the report.

7. In your application, you can now change the location of the image file at run time, by modifying the text in the "Location" formula programmatically using the following code:

myReport.DataDefinition.FormulaFields["Location"].Text = "'" + "C:\Images\Image2.jpg"+ "'"


You can now view the image successfully when previewing your report in the application.