Click here to Skip to main content
Click here to Skip to main content
Go to top

Custom Field in Crystal Report

, 6 Aug 2008
Rate this:
Please Sign up or sign in to vote.
This application will adjust the position of the fields in Crystal report, which are dragged and dropped during design time

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.

Points of Interest

I learnt a lot about Crystal reports while writing this code. Wherever I searched I got one comment, "You cannot change the fields of Crystal report dynamically". At one place, someone wrote that it is impossible. Impossible itself says that "I am possible". Happy coding!

History

  • 6th August, 2008: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kashif Abbas
Software Developer (Senior) Ciklum
Pakistan Pakistan
No Biography provided

Comments and Discussions

 
Questionhow to change this application blocks to ado objects Pinmemberk.sathiyajeevan30-Jul-12 18:53 
GeneralMy vote of 5 PinmemberRitesh Khatri22-Feb-12 21:00 
QuestionHow to fetch value of a field PinmemberRicha Sharma from Chandigarh20-Jan-11 0:37 
Generalyour package cann't be downloaded . PinmemberAlaa Jebran19-Jan-09 6:33 
GeneralRe: your package cann't be downloaded . PinmemberKashif Abbas19-Jan-09 19:39 
GeneralCustomizing the Cross tab crystal report Pinmemberda_dhanaraj5-Nov-08 5:40 
GeneralCustomizing the crystal reports in C#.net windows application Pinmemberda_dhanaraj4-Nov-08 8:40 
GeneralSuppress the Data Fields in Crystal Reports Pinmemberda_dhanaraj3-Nov-08 4:08 
GeneralRe: Suppress the Data Fields in Crystal Reports [modified] PinmemberKashif Abbas3-Nov-08 19:40 
GeneralSuppress the Data Fields in Crystal Reports Pinmemberda_dhanaraj1-Nov-08 10:10 
GeneralRe: Suppress the Data Fields in Crystal Reports PinmemberKashif Abbas2-Nov-08 18:09 
GeneralRe: Suppress the Data Fields in Crystal Reports Pinmemberda_dhanaraj3-Nov-08 1:12 
GeneralRe: Suppress the Data Fields in Crystal Reports [modified] PinmemberKashif Abbas3-Nov-08 19:43 
GeneralSuppress the Print date at run time Pinmemberda_dhanaraj1-Nov-08 8:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 7 Aug 2008
Article Copyright 2008 by Kashif Abbas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid