Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

Dynamic Crystal Report with C#

Download source - 57.25 KB

Introduction
This program shows how to dynamically load data from a database and application into the Crystal eport. By using this program! we can customi"e a Crystal eport to some limit at run time of the application li#e specifying which field $Columns of a particular table% should be displayed in the report.

Background
This problem arose because a group of students from &'((T as#ed me how to dynamically generate a Crystal eport using C) 2.* $.+,T 2**5%. ( tried to find a solution for this by searching many forums and sites! but unfortunately ( couldn-t find any solution for that. &ome forums said that there is no way to create dynamic Crystal eports using .+,T 2**5. .inally! ( found a way to do that.

Using the Code


/. Create a C) pro0ect or add a .orm to your e1isting pro0ect. +ow you can add Chec#bo1es that correspond to columns of a particular table that should be displayed in the Crystal eport and Crystal eport2iewer control to the form.

.or this demonstration! ( ha3e created a database called db1.mdb $in bin\Debug% using 4ccess and created a table called Customer.

2. 4dd a DataSet $*.xsd file% to your pro0ect using add -5 +ew (tems in solution e1plorer. 4fter that! add a DataTable to the DataSet.

4dd columns to DataTable and name them Column/! Column2! and so on. The number of columns depends on how many columns should be displayed in the Crystal report. 6. 4dd a Crystal eport into the pro0ect and using the eport 7i"ard! choose 4D8.+,T DataSets of the 9ro0ect data source as the data source of the Crystal eport and select Customer data table of DataSet1 as the selected table of the Crystal eport. Then select fields to be displayed in your report. Then remo3e Column1:! Column5 ob0ects in &ection 2 of the Crystal eport.

;. +ow add parameters called col1! col2: col5 $the number of parameters should be e<ual to the number of columns displayed in the Crystal eport.% using .ield ,1plorer.

5. 4dd the following method to your .orm for Create &=' &,',CT <uery and assign 3alues to parameters of the Crystal eport according to user selected columns that should be displayed on your report. Collapse
>>> ?summary5 >>> This method is used to >>> /. create &,',CT <uery according to the selected column names and >>> 2. create parameters and assign 3alues for that parameter >>> that correspond to the crystal report. >>> +8T,@ This parameter is used to display Column names of the >>> Crystal eport according to the user selection. >>> ?>summary5 >>> ?returns5?>returns5 pri3ate string Create&elect=uery4nd9arameters$% A eportDocument reportDocumentB 9arameter.ields param.ieldsB 9arameter.ield param.ieldB 9arameterDiscrete2alue paramDiscrete2alueB reportDocument C new eportDocument$%B param.ields C new 9arameter.ields$%B string <uery C D&,',CT DB int column+o C *B if $chbCode.Chec#ed% A

column+oEEB <uery C <uery.(nsert$<uery.'ength! DCode as ColumnD E column+o.To&tring$%%B param.ield C new 9arameter.ield$%B param.ield.+ame C DcolD E column+o.To&tring$%B paramDiscrete2alue C new 9arameterDiscrete2alue$%B paramDiscrete2alue.2alue C DCustomer CodeDB param.ield.Current2alues.4dd$paramDiscrete2alue%B >>4dd the param.ield to param.ields param.ields.4dd$param.ield%B F if $chb.irst+ame.Chec#ed% A column+oEEB if $<uery.Contains$DColumnD%% A <uery C <uery.(nsert$<uery.'ength! D! D%B F <uery C <uery.(nsert$<uery.'ength! D.irst+ame as ColumnD E column+o.To&tring$%%B param.ield C new 9arameter.ield$%B param.ield.+ame C DcolD E column+o.To&tring$%B paramDiscrete2alue C new 9arameterDiscrete2alue$%B paramDiscrete2alue.2alue C D.irst +ameDB param.ield.Current2alues.4dd$paramDiscrete2alue%B >>4dd the param.ield to param.ields param.ields.4dd$param.ield%B F if $chb'ast+ame.Chec#ed% A column+oEEB >>To determine Column number if $<uery.Contains$DColumnD%% A <uery C <uery.(nsert$<uery.'ength! D! D%B F <uery C <uery.(nsert$<uery.'ength! D'ast+ame as ColumnD E column+o.To&tring$%%B param.ield C new 9arameter.ield$%B param.ield.+ame C DcolD E column+o.To&tring$%B paramDiscrete2alue C new 9arameterDiscrete2alue$%B paramDiscrete2alue.2alue C D'ast +ameDB param.ield.Current2alues.4dd$paramDiscrete2alue%B >>4dd the param.ield to param.ields param.ields.4dd$param.ield%B F if $chb4ddress.Chec#ed% A column+oEEB if $<uery.Contains$DColumnD%% A <uery C <uery.(nsert$<uery.'ength! D! D%B F <uery C <uery.(nsert$<uery.'ength! D4ddress as ColumnD E

column+o.To&tring$%%B param.ield C new 9arameter.ield$%B param.ield.+ame C DcolD E column+o.To&tring$%B paramDiscrete2alue C new 9arameterDiscrete2alue$%B paramDiscrete2alue.2alue C D4ddressDB param.ield.Current2alues.4dd$paramDiscrete2alue%B >>4dd the param.ield to param.ields param.ields.4dd$param.ield%B F if $chb9hone.Chec#ed% A column+oEEB if $<uery.Contains$DColumnD%% A <uery C <uery.(nsert$<uery.'ength! D! D%B F <uery C <uery.(nsert$<uery.'ength! D9hone as ColumnD E column+o.To&tring$%%B param.ield C new 9arameter.ield$%B param.ield.+ame C DcolD E column+o.To&tring$%B paramDiscrete2alue C new 9arameterDiscrete2alue$%B paramDiscrete2alue.2alue C D9honeDB param.ield.Current2alues.4dd$paramDiscrete2alue%B >>4dd the param.ield to param.ields param.ields.4dd$param.ield%B F >>if there is any remaining parameter! assign empty 3alue for that >>parameter. for $int i C column+oB i ? 5B iEE% A column+oEEB param.ield C new 9arameter.ield$%B param.ield.+ame C DcolD E column+o.To&tring$%B paramDiscrete2alue C new 9arameterDiscrete2alue$%B paramDiscrete2alue.2alue C DDB param.ield.Current2alues.4dd$paramDiscrete2alue%B >>4dd the param.ield to param.ields param.ields.4dd$param.ield%B F crystal eport2iewer/.9arameter.ield(nfo C param.ieldsB <uery EC D . 8G CustomerD B return <ueryB F >>

H. 4dd the following method to the button clic# e3ent to display a report when the user presses the button@ Collapse

using &ystemB using &ystem.Collections.IenericB using &ystem.ComponentGodelB using &ystem.DataB using &ystem.DrawingB using &ystem.Te1tB using &ystem.7indows..ormsB using &ystem.Data.8leDbB using CrystalDecisions.Crystal eports.,ngineB using CrystalDecisions. eport&ourceB using CrystalDecisions.&haredB using CrystalDecisions.7indows..ormsB namespace app5 A public partial class .orm/ @ .orm A Crystal eport/ ob0 ptB public .orm/$% A (nitiali"eComponent$%B F pri3ate 3oid button/JClic#$ob0ect sender! ,3ent4rgs e% A ob0 pt C new Crystal eport/$%B string conn&tring C D9ro3iderCGicrosoft.Ket.8',DB.;.*BD E DData &ourceCLDataDirectoryLMMdb/.mdbDB >>Iet &elect <uery &tring and add parameters to the >>Crystal report. string <uery C Create&elect=uery4nd9arameters$%B >>if there is no item select! then e1it from the method. if $N<uery.Contains$DColumnD%% A GessageBo1.&how$D+o selection to displayND%B returnB F try A 8leDbConnection Conn C new 8leDbConnection$conn&tring%B 8leDbData4dapter adepter C new 8leDbData4dapter$<uery! conn&tring%B Data&et/ Ds C new Data&et/$%B adepter..ill$Ds! DCustomerD%B ob0 pt.&etData&ource$Ds%B crystal eport2iewer/. eport&ource C ob0 ptB F

catch $8leDb,1ception ole,1% A GessageBo1.&how$ole,1.Gessage%B F catch $,1ception ,1% A GessageBo1.&how$,1.Gessage%B F F

You might also like