Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

Custom Field in Crystal Report

Rate me:
Please Sign up or sign in to vote.
4.75/5 (17 votes)
6 Aug 2008CPOL3 min read 115.5K   5.9K   45   19
This application will adjust the position of the fields in Crystal report, which are dragged and dropped during design time

Image 1

Before Fields adjustment

Image 2

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:

C#
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:

C#
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.

C#
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)


Written By
Software Developer (Senior) Ciklum
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseAwesome. Pin
S.N.Ramkumar25-Apr-19 3:18
S.N.Ramkumar25-Apr-19 3:18 
GeneralMy vote of 5 Pin
S.N.Ramkumar25-Apr-19 3:08
S.N.Ramkumar25-Apr-19 3:08 
Questionhow to change this application blocks to ado objects Pin
k.sathiyajeevan30-Jul-12 18:53
k.sathiyajeevan30-Jul-12 18:53 
GeneralMy vote of 5 Pin
Ritesh Khatri22-Feb-12 21:00
Ritesh Khatri22-Feb-12 21:00 
QuestionHow to fetch value of a field Pin
Sharma Richa20-Jan-11 0:37
Sharma Richa20-Jan-11 0:37 
Generalyour package cann't be downloaded . Pin
Alaa Jubran19-Jan-09 6:33
Alaa Jubran19-Jan-09 6:33 
GeneralRe: your package cann't be downloaded . Pin
Kashif Abbas19-Jan-09 19:39
Kashif Abbas19-Jan-09 19:39 
I'v checked the project. It is downloading and working properly.

Kashif Abbas

GeneralCustomizing the Cross tab crystal report Pin
da_dhanaraj5-Nov-08 5:40
da_dhanaraj5-Nov-08 5:40 
GeneralCustomizing the crystal reports in C#.net windows application Pin
da_dhanaraj4-Nov-08 8:40
da_dhanaraj4-Nov-08 8:40 
GeneralSuppress the Data Fields in Crystal Reports Pin
da_dhanaraj3-Nov-08 4:08
da_dhanaraj3-Nov-08 4:08 
GeneralRe: Suppress the Data Fields in Crystal Reports [modified] Pin
Kashif Abbas3-Nov-08 19:40
Kashif Abbas3-Nov-08 19:40 
GeneralSuppress the Data Fields in Crystal Reports Pin
da_dhanaraj1-Nov-08 10:10
da_dhanaraj1-Nov-08 10:10 
GeneralRe: Suppress the Data Fields in Crystal Reports Pin
Kashif Abbas2-Nov-08 18:09
Kashif Abbas2-Nov-08 18:09 
GeneralRe: Suppress the Data Fields in Crystal Reports Pin
da_dhanaraj3-Nov-08 1:12
da_dhanaraj3-Nov-08 1:12 
GeneralRe: Suppress the Data Fields in Crystal Reports [modified] Pin
Kashif Abbas3-Nov-08 19:43
Kashif Abbas3-Nov-08 19:43 
GeneralSuppress the Print date at run time Pin
da_dhanaraj1-Nov-08 8:29
da_dhanaraj1-Nov-08 8:29 
GeneralZip file damaged Pin
cakirhal12-Aug-08 2:43
cakirhal12-Aug-08 2:43 
GeneralRe: Zip file damaged Pin
Kashif Abbas12-Aug-08 19:43
Kashif Abbas12-Aug-08 19:43 
GeneralExcellent Effort Dude! Pin
usman_dastgeer7-Aug-08 18:28
usman_dastgeer7-Aug-08 18:28 

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

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