Pages

Friday, November 25, 2011

Custom Field in Crystal Report


Before Fields adjustment

After Fields adjustment

Introduction

This article will help you understand how to dynamically adjust the fields on the Crystal report that you've already dragged and dropped on the Crystal report during design time and for which, you would like to adjust their position during runtime.

Background

Now-a-days, I'm working in Windows applications in C#. I created a Crystal report and showed that to the client, he was very much satisfied with the working generation of the report. On the very next day, he said that he'd like to see the fields that the user selects. As I'd already spent many days on designing that report, I did not want to change that all over again.
The problem occurs when the user selects the last fields. When the last fields are selected, then the starting fields are left blank. I've searched it on the internet, including The Code Project and many of the search engines, but couldn't find any help regarding this.
So when I solved my problem, I thought I must share this with other people. As this is first version of this article, I expect a lot of suggestions from all of you.

Using the Code

The attached zip file contains all the code needed to run the application. One thing I'd like to mention is that in the sqlregistrationprovider class, don't get confused on seeing the following statements:
SqlDatabase _database = null;
DbCommand _command = null;
I've used Application Blocks, which contain these two classes. You can use ADO.NET objects to do the database related tasks. PatientReport2.cs has lots of bool variables to adjust the fields in Crystal report.
showFields is the function that is used to set the bool values to hide or display the fields on the report, as selected by the user, number of bool in the class is equal to the number of fields in the Crystal reports.
If the user does not want to see the fullname, i.e. if the Fullname checkbox is unchecked, the bool showFullName will be set to false and the following lines of code will hide the field in the Crystal report:
if (!showFullName)
{
  crystalReport21.Section2.ReportObjects["FullName1"].ObjectFormat.EnableSuppress = true;
  crystalReport21.Section2.ReportObjects
 ["FullNameText"].ObjectFormat.EnableSuppress = true;
 }  
FullName1 is the name of the field in the Crystal report's section 3 (Detail Section) and FullNameText is the text/label that will be shown in the report's section 2 (Page Header).
The main function that is doing the work is SetSuppressFields(string, string). It takes two parameters, the first one is the field name under section 2 (Detail Section), and second one is the text under Section 2 (Page Header) of the Crystal report.
One requirement to use this functionality is that you should know all the fieldName (Section 3) and fieldText (Section 2).

If you don't know where to find these names, open the crystalreport-> click on the field and press F4, or right-click on the field and select properties, and note down the (Name) attribute.
 if (!crystalReport21.ReportDefinition.Sections[2].ReportObjects
 ["contactnumber1"].ObjectFormat.EnableSuppress && !contactNo)
            {
                crystalReport21.ReportDefinition.Sections[2].ReportObjects
  ["contactnumber1"].Left = crystalReport21.
  ReportDefinition.Sections[2].ReportObjects[fieldName].Left;
                crystalReport21.ReportDefinition.Sections[2].ReportObjects
  ["contactnumbertext"].Left = crystalReport21.ReportDefinition.
  Sections[2].ReportObjects[fieldText].Left;
                contactNo = true;
            } 
contactNo is a bool variable that shows whether the contactnumber field in the Crystal report is set during runtime or not. By set, I mean that if the starting fields are not shown, then the last fields will be replaced by it, which is also shown in the attached images.

No comments:

Post a Comment