Escolar Documentos
Profissional Documentos
Cultura Documentos
Authors:
(2
' $in#crac)er LL*. +ll rig"ts reserve#. ,nite# States an# -nternational cop.rig"t laws protect t"is propert.. $in#crac)er LL* or t"e aut"or of t"is material "as no affiliation wit" $icrosoft or an. ot"er companies t"at #evelops t"e software. Some )e.wor#s use# in t"is material are registere# tra#emar)s of t"eir respective owners. Repro#uction or printing multiple copies of t"is material is pro"i!ite#. -f .ou nee# multiple copies of t"is material& please contract authors@c-sharpcorner.com.
*op.rig"t 2
ntroduction
T"is #ocument #iscusses t"e ReportViewer control an# "ow to generate reports using t"e ReportViewer control. T"is #ocument covers t"e following topics: ,n#erstan#ing t"e ReportViewer control an# report processing mo#es *reating a simple report /enerating reports from an o!0ect collection /enerating reports from a 1ataSet /enerating reports from an 2$L #ocument
*op.rig"t 2
6igure 9. 6. -t will a## Report.r#lc file to .our pro0ect an# will open t"e report #esigner& w"ic" loo)s li)e 6igure 2. +s .ou can see in t"e left si#e& .ou will see .our 1ataSet.
6igure 2. 7. Now .ou can e:pan# .our 1ataSet an# #rag w"atever column .ou want in t"e report to t"e report #esigner. select t"ree columns in 6igure %.
6igure %. 8. Now if .ou rig"t clic) an# select Properties on a Te:t7o:& .ou will see 6igure ;& w"ere .ou can appl. settings li)e visi!ilit.& navigation& format& font& an# sorting. 6or now& .ou can sa. ?@ on t"is #ialog. - will #iscuss t"ese properties in more #etails in m. following articles.
*op.rig"t 2
6igure ;. 9. Now go to t"e 5e! page an# clic) t"e smart tag on ReportViewer an# select Report.r#lc from t"e list. See 6igure 4.
6igure 4. 9 . T"at8s it. Now if .ou run t"e application& .ou will see report. Aou can navigate t"roug" recor#s& an# e:port it to E:cel or P16 using t"e E:port option.
*op.rig"t 2
Listing 9. Now - !uil# a *ompan. class& w"ic" is a generic collection of Emplo.ee wit" t"ree recor#s in it. T"e *ompan. class is liste# in Listing 2.
class $ompany { private %ist&Employee' m(employees; public $ompany(! { m(employees = ne) %ist&Employee'(!; m(employees#Add(ne) Employee(*+a"es" $"and* *11, Ne) -oad $"adds .ord /A* *1,01,11 02!!; m(employees#Add(ne) Employee(*3ac4 +o"ita* */ear %ane Ne) 5or4 ,0,01* *67611,1 ,0!!; m(employees#Add(ne) Employee(*-enee Singer* *Near medo) /"iladelp"ia /A* *9621221 ,2!!; } public %ist&Employee' :etEmployees(! { return m(employees; }
Listing 2. -n Listing 2& t"e /etEmplo.ees met"o# returns all t"e emplo.ees in t"e compan..
*op.rig"t 2
6igure 9. -t will launc" t"e 1ata Source *onfiguration 5i<ar#. Select ?!0ect item on t"e first page. See 6igure 2.
6igure 2. Now on ne:t screen .ou s"oul# see .our application namespace. -f .ou #on8t see t"e namespace& ma)e sure to Re!uil# t"e application. -f .ou e:pan# t"e namespace& .ou s"oul# see Emplo.ee class. See 6igure %. Select Emplo.ee an# clic) Ne:t an# clic) 6inis" on t"e ne:t screen.
*op.rig"t 2
6igure %. Now .ou s"oul# see Emplo.ees in 1ata Sources win#ow. See 6igure ;.
6igure ;. Note: 5"en .ou a## a 1ataSource to t"e pro0ect& t"e #esigner a##s t"e Emplo.ee7in#ingSource to t"e #esigner& w"ic" will pla. a ma0or role later.
*op.rig"t 2
6igure 4. Now .ou can format t"e ta!le t"e wa. .ou want !. rig"t clic)ing on t"e ta!le& column& or a cell an# select Properties item.
6igure D.
*op.rig"t 2
T"e 1ata
- "ave an 2$L file 1ata.:ml& w"ic" loo)s li)e t"is: FG:ml versionHI9. I enco#ingHIutfEJI GB - F*ompan.B - FEmplo.eesB FEmplo.ee NameHI(ahesh ChandI +geHI+0I P"oneHI,-0-2+++++I 3B FEmplo.ee NameHIRose !arnerI +geHI5,I P"oneHI2-++.2/00/I 3B FEmplo.ee NameHIAmger 1apI +geHI22I P"oneHI2022+.2/00I 3B FEmplo.ee NameHI(i3e !oldI +geHI+5I P"oneHI220/0///2+I 3B FEmplo.ee NameHIRenee 4lowerI +geHI-2I P"oneHI././20-00+I 3B F3Emplo.eesB F3*ompan.B
$. first goal is to generate sc"ema file& w"ic" will represent t"e #ata. - ta)e "elp of t"e 1ataSet class an# its met"o# 5rite2mlSc"ema. T"e co#e liste# in Listing 9 rea#s t"e 1ata.:ml file an# generates a sc"ema file calle# 1ata.:s#. T"is file is create# in t"e Debug fol#er of .our application. 1ataSet #s H new 1ataSetKLM #s.Rea#2mlKI1ata.:mlILM #s.5rite2mlSc"emaKI1ata.:s#ILM Listing 9. Let8s a## 1ata.:s# file to .our pro0ect. Rig"t clic) on t"e pro0ect in Solution E:plorer& select +## BB E:isting -tem an# !rowse for 1ata.:s# file an# a## it to t"e pro0ect. Now let8s Re!uil# t"e pro0ect.
Now we will a## a new report file to t"e pro0ect. Rig"t clic) on t"e pro0ect in Solution E:plorer an# select +## BB New -tem an# select Report from t"e -tems list. -t will a## Report9.r#lc file to t"e pro0ect. ?nce t"e report is a##e#& our ne:t step is to a## a #ata source. 6irst #ou!le clic) on t"e 6orm an# select 1ata $enu item from t"e $ain $enu. *lic) on 1ata $enu item an# select +## New 1ata Source item. -t will launc" 1ata Source *onfiguration 5i<ar#. Select ?!0ect from t"e list an# clic) t"e Ne:t !utton on t"e 5i<ar#. See 6igure 2.
*op.rig"t 2
6igure %. Select *ompan. class an# clic) t"e Ne:t !utton. ?n ne:t #ialog& .ou will see a confirmation message. Select 6inis" t"ere an# get out of t"e wi<ar#. Now #ou!le clic) on t"e Report9.r#lc file an# .ou s"oul# see 6igure ; in .our 1ata Sources win#ow.
6igure ;. Now let8s create an# format t"e report. 1rag a Ta!le from t"e Tool!o: an# #rag Name& +ge& an# P"one columns from t"e 1ata Sources to t"e report8s mi##le row. +s .ou can see from 6igure 4& t"e name of t"e column is automaticall. a##e# to t"e "ea#er KfirstL row of t"e report.
*op.rig"t 2
10
6igure 4.
6igure D. 7. #oing so& .ou will see an Emplo.ee7in#ingSource control is a##e# at t"e !ottom of t"e 6orm. See 6igure '. T"e 7in#ingSource control provi#es connection !etween t"e #ata an# t"e ReportViewer control.
6igure '.
Now write t"e co#e liste# in Listing 2 on t"e 6orm8s loa# event "an#ler. T"is co#e creates a 1ataSet& loa#s t"e #ata from 1ata.:ml file an# sets Emplo.ee7in#ingSource.1ataSource as 1ataSet. T"e last line is a##e# !. .ou !. t"e #esigner. =ataSet ds = ne) =ataSet(!; ds#-ead@ml(*=ata#Aml*!; Employee<indingSource#=ataSource = ds; t"is#report>ie)er1#-e?res"-eport(!; Listing 2.
T"at8s all. 7uil# an# run t"e application. T"e output s"oul# loo) li)e 6igure J.
*op.rig"t 2
11
6igure J.
*op.rig"t 2
12
6irst of all& we nee# a t.pe# 1ataSet t"at will represent t"e #ata t.pes. Aou can generate a t.pe# 1ataSet from a #ata!ase ta!le& store# proce#ure& an# a view or from a SQL =uer.. +fter generating t"e t.pe# 1ataSet& report generation is same. Rig"t clic) on t"e pro0ect in Solution E:plorer& select +## BB New -tem an# select 1ataSet from t"e list. *"ange 1ataSet9.:s# to *ompan..:s#. +fter a##ing t"e 1ataSet& .ou will see t"e #esigner. ?n t"e #esigner& clic) Server E:plorer lin)& w"ic" will let .ou create a 1ata!ase connection an# can view #ata!ase o!0ects inclu#ing ta!les& views& an# store# proce#ures. 1rag a #ata!ase ta!le& or view& or store# proce#ure w"ere .ou want to #ispla. t"e #ata from an# it will create a 1ataSet sc"ema for .ou. ?nce .ou "ave a t.pe# 1ataSet& 0ust follow t"ese steps. 7efore t"at& ma)e sure .ou Re!uil# t"e pro0ect.
Now we will a## a new report file to t"e pro0ect. Rig"t clic) on t"e pro0ect in Solution E:plorer an# select +## BB New -tem an# select Report from t"e -tems list. -t will a## Report9.r#lc file to t"e pro0ect. ?nce t"e report is a##e#& our ne:t step is to a## a #ata source. 6irst #ou!le clic) on t"e 6orm an# select 1ata $enu item from t"e $ain $enu. *lic) on 1ata $enu item an# select +## New 1ata Source item. -t will launc" 1ata Source *onfiguration 5i<ar#. Select ?!0ect from t"e list an# clic) t"e Ne:t !utton on t"e 5i<ar#. See 6igure 2.
6igure 2. ?n ne:t #ialog& .ou s"oul# see all namespaces an# classes in .our pro0ect. E:pan# .our namespace an# .ou will see class *ompan.. See 6igure %.
*op.rig"t 2
13
6igure %. Select *ompan. class an# clic) t"e Ne:t !utton. ?n ne:t #ialog& .ou will see a confirmation message. Select 6inis" t"ere an# get out of t"e wi<ar#. Now #ou!le clic) on t"e Report9.r#lc file an# .ou s"oul# see 6igure ; in .our 1ata Sources win#ow.
6igure ;. Now let8s create an# format t"e report. 1rag a Ta!le from t"e Tool!o: an# #rag Name& +ge& an# P"one columns from t"e 1ata Sources to t"e report8s mi##le row. +s .ou can see from 6igure 4& t"e name of t"e column is automaticall. a##e# to t"e "ea#er KfirstL row of t"e report.
*op.rig"t 2
14
6igure 4.
6igure D. 7. #oing so& .ou will see an Emplo.ee7in#ingSource control is a##e# at t"e !ottom of t"e 6orm. See 6igure '. T"e 7in#ingSource control provi#es connection !etween t"e #ata an# t"e ReportViewer control.
6igure '.
Now write t"e co#e liste# in Listing 2 on t"e 6orm8s loa# event "an#ler. T"is co#e creates a 1ataSet& loa#s t"e #ata from 1ata.:ml file an# sets Emplo.ee7in#ingSource.1ataSource as 1ataSet. T"e last line is a##e# !. .ou !. t"e #esigner. 1ataSet #s H new 1ataSetKLM #s.Rea#2mlKI1ata.:mlILM Emplo.ee7in#ingSource.1ataSource H #sM t"is.reportViewer9.Refres"ReportKLM Listing 2.
T"at8s all. 7uil# an# run t"e application. T"e output s"oul# loo) li)e 6igure J.
*op.rig"t 2
15
6igure J.
*op.rig"t 2
16
6igure 9. - #efinite t"e following varia!le in t"e class: private bool ?irst.ound = ?alse; T"e following co#e written on t"e 6in# 7utton clic) event "an#ler. private void .ind<utton($lic4(ob;ect sender EventArgs e! { i? (?irst.ound! t"is#report>ie)er1#.indNeAt(!; else i? (t"is#report>ie)er1#.ind(.indBeAt<oA#BeAt 1! '= 2! ?irst.ound = true; } Now if - t.pe N*"an#O in t"e Te:t7o: an# clic) t"e 6in# !utton& t"e first row is selecte#. *lic)ing 6in# again select t"e ne:t row t"at "as te:t *"an# in it. See 6igure 2.
6igure 2.
*op.rig"t 2
17
*op.rig"t 2
18