Vous êtes sur la page 1sur 29

Bi 1: Tng quan v Crystal Reports .NET 1.1 Crystal Reports l g?

Crystal Reports l cng c thit k bo co cho php bn to ra nhng bo co bng cch tm v nh dng d liu t mt ngun d liu hay t nhng ngun d liu khc nhau .Bn cnh , Crystal Reports c mt ngn ng ring tnh ton v mt s tnh nng khc bin nhng d liu th thnh nhng bo co y tnh chuyn nghip.Bn c th to nhng bo co i t nhng danh sch n gin ch gm vi ct cho n nhng bo co phc tp c km biu ,bng v ch s Key Performance Indicator (KPI).Ngoi ra , Crystal Reports c mt s hm API cng nh cng c c bit c thit k dnh cho cc nh pht trin phn mm cho php hi nhp cc bo co ny vo trong ng dng ring ca h. 1.2 c im ca Crystal Reports -Bn khng cn m mt ng dng ring r thit k bo co khi dng Crystal Reports -i vi Windows Form , Crystal Reports cho php xem bo co v cung cp tt c cc chc nng cho ngi s dng bao gm xoy su vo chi tit ,truy xut ,xut khui vi ASP.NET ,cung cp phn ln cc chc nng trong Windows Form Viewer trong mi trng DHTML zero client (Ngha l khng client no c np xung hay ci t). -Crystal Reports gip vic truy cp d liu tr nn d dng hn. -Crystal Reports cho php truy cp nhng d liu ngun khc nhau. -Crystal Reports cho php chia s s dng cc bng bo co cng nh to nhng ng dng c phn phi s dng cho nhiu ngi dng.

Chng 2: Thit k mt bo co s dng Report Design Environment Trong chng ny ta s ln lt s dng Report Designer bn trong Visual Studio.NET thit k mt bo co n gin. Bn s thy trong chng ny bn ch n gin l click chn v ko th l c th hon thnh mt bo co m khng cn phi lp trnh g c. 2.1 Cc thnh phn Report Designer Trn Crystal Reports.NET cung cp cho bn cc ca s v cc thanh cng c gip bn thun tin hn trong khi thit k bo co nh hnh 2-01.

Crystal Reports Insert

Crystal Reports Main

Crystal Reports Toolbox

Hnh 2-01: Cc thnh phn ca Report Designer Thanh cng c Crystal Reports Main cha cc control nh dng nh Font, Size... .Thanh cng c Crystal Reports Insert cho php bn chn vo Summary, Group, Subreport, Chart v Picture. Hp cng c Crystal Reports cho php bn thm vo cc i tng Text, Line v Box. Mun thm mt Field vo bo co, th c ca s Field Explorer. Bn ch cn bung cc nhnh tng ng trn ca s ny v ko th Field no bn cn ln bn bo co. Cui cng l ca s Report Designer, y chnh l ni bn thit k bn bo co, n bao gm cc section, trn mi section s cha cc field, cc i tng bo co. 2.2 Cc vn c bn thit k bo co 2.2.1 Cc Section ca mt bo co M t Report Header Xut hin trn trang u tin ca bn bo co v thng b hu theo mc nhin .C th c dng cho bit bt u ca mt bn bo co mi hay dng lm trang ba Report Footer Xut hin vo cui trang cht ca bng bo co,theo mc nhin l cho thy v c th dng tng kt bo co(ghi s mu tin ,ngy in bo co ,tn tp tin,) Page Header

Xut hin trn u mi trang (khi sang trang) v c th dng ghi ta bo co,tiu cc ct ,s trang Page Footer Xut hin vo cui mi trang v c th dng in s trang ngy in bo co, Group Header Xut hin trn u mi nhm (khi c ngt cp bc) v thng ghi tn nhm mi Group Footer Xut hin vo cui mt nhm mu tin , v thng c dng in ra tn nhm c ,tng cng ca nhm hay tng kt Details Xut hin i vi mi mu tin thng c dng in thng tin ca ct v c th c bung thnh section cc vng mc tin ln hn hoc to biu mu 2.2.2 Cc i tng trong Field Explorer:

-Database Fields: Cha tt c cc field m bn a vo bn bo co, cc field ny c th c ly t Table, View hay Stored Procedures. Cc field c a vo bn bo co th mt du check s hin ln bn cnh, cho bit field ny ang c s dng. -Formula Fields: dng a cc tnh ton phc tp vo bo co.C 2 c php phi chn l Crystal syntax , Basic syntax. -Parameter Fields: dng nhc nh ngi dng nhp vo cc thng tin cn thit i vi bo co ang chy. Khi bn to mt parameter field v a vo bo co,th Crystal Reports .NET s hin th mt khung i thoi mc nhin nhc nh ngi dng nhp vo chi tit m bn va nhp vo ,khi no bn bo co ca bn c xem trc. -Running Total Field: dng tnh ton da trn gi tr cc mu tin ca mt field khc. -Group Name Fields: Th hin cc nhm ang c s dng trong bo co. -SQL Experssion Fields: m bo vic tnh ton ca bn c hin trn server v bn c th thm nhp vo tt c cc hm ca SQL. -Special Fields: th hin cc vng mc tin c bit. Bng 2-02 sau y lit k cc field c bit. Bng 2-02 Danh sch cc field c bit

Tn field c bit M t Print Date In ra ngy bo co c in Print Time In ra thi gi khi bo co c in ra Modification Date Ngy thay i cht ca bo co Modification Time Thi gi thay i cht ca bo co Data Date Ngy khi d liu c c t cn c d liu Data Time Thi gi khi d liu c c t cn c d liu Record Number Con s th t ni b ca mu tin c gn cho tt c cc mu tin c tr v cho bn bo co Page Number S th t trang Group Number S th t ni b nhm Total Page Count Tng s trang m c Report Title Ta bo co nh c lu tr trong Summary Infomationca tp tin bo co Report Comments Ch gii s c a vo trong Summary Information ca tp tin bo co. Record Selection Formula Cng thc tuyn chn mu tin c s dng bi bo co Group Selection Formula Cng thc tuyn chn nhm c s dng bi bo co File Path and Name ng dn v tn tp tin ca .rpt File Author Tc gi bo co s c a vo trong Summary Information ca tp tin bo co. File Creation Date Ngy bn bo co c to ra Pagd N of M Theo y N l trang hin hnh v M l tng s trang bo co

-Unbound Fields: gm c 7 loi kiu d liu Boolean, Currency, Date, Date Time, Number, String , v Timer. Unbound dng to mt bn bo co generic ri dng lp trnh cho t ni dung cc vng mc tin ny vo lc chy. 2.3 Thit k mt bo co n gin: V d sau y s gip bn to ra mt bo co n gin, cc bn ch vic click v click to ra mt bn bo co m khng cn phi lp trnh g c. Ty theo c trng ca tng bn bo co m bn c th b qua mt s bc khng cn thit, nhng gip bn hiu r hn cc bc nn vi v d ny chng ta s kho st tt c cc bc mc d c mt s bc chng ta khng lm g c m ch nhn Next qua bc k tip. 2.3.1 Thm mt bo co mi vo ng dng Khi ng Microsoft Visual Studio .NET v to mt project mi. Sau , chn Project | Add New Item s xut hin hp thoi Add New Item (Hnh 2-03). Trn khung Templates, bn chn Crystal Report v t trn bo co l rptBaocao.rpt, ri chn Open.

Hnh 2-03 Hp thoi Add New Item Hp thoi Crystal Report Gallery s hin th (Hnh 2-04). Trn khung Create a New Crystal Report Document c ba la chn c gii thch trong Bng 2-03. V trn khung Choose an Expert c by la chn c gii thch trong Bng 2-04. Bn cc la chn ny theo gi tr mc nh ban u v nhn OK.

Hnh 2-04: Hp thoi Crystal Report Gallery Bng 2-03: Cc phng php to mt Crystal Report Document mi Phng php M t Using the Report Expert Nh bn c th thy trong hnh 2-04 c nhiu Report Expert (Standard,Form Letter Form,) m bn phi bc qua tng bc to bo co .y l phng php ph bin nht nh l im xut pht cho nhng trin khai v sau.Lp trnh vin kinh nghim cng thch s dng phng php ny. As a Blank Report i vi lp trnh vin kinh nghim qu quen thuc vi Report Designer thng dng phng php ny to bo co rng v t tay mnh b sung tng phn vo bn bo co khng cn s h tr ca Report Wizard. Khi mc chn ny c chn th cc Expert khc s khng c sn. From an Existing Report To mt bo co mi tng t mt bn bo co c sn. Mt ln na khi chn mc ny th cc Expert khc s khng c sn. Bng 2-04: Cc loi Expert Report Loi Expert

M t Standard c s dng thng xuyn v mang tnh chung (generic) nhiu nht. Bn c th s dng Standard Expert to kiu bo co kiu hng ct (tabular), bao gm mt s tnh nng nh grouping (gp nhm), sorting (sp xp) v summary (tng kt) . Standard Expert cn bao gm kh nng thm cc hnh ho kiu thng k c gi l chart, p dng mt s style nh sn v sng lc cc mu tin cng nh c nhng tnh nng phn tch cao cp chng hn TopN, BottomN (th d Top10 hoc Bottom100). From Letter Bng cch phi hp cc i tng vn bn v cc Field CSDL, Crystal Reports.NET c th dng to nhng vn th (form letter). Form Dng to nhng bn bo co c thit k cho mt biu mu t trng (ho n, bng k khai, ...). Bng cch s dng form expert, bn c kh nng cho nm n mt hnh nh di bn bo co (v d nh mt ho n trng) c th a vo ng ch cc field trn biu mu.T y, bn c th hoc yn hnh nh v in ra biu mu km theo cc vng mc tin ln giy hoc cho g b hnh nh v in trc tip bn bo co ln cc biu mu. Cross-Tad Cross-tabs trong lng Crystal Reports.NET trng ging nh mt bn tnh (spreadsheet) vi hng ct cha d liu tng kt. Bng cch s dng Cross-tab Expert bn c th to mt bn bo co vi i tng cross-tab trong section report header. Subreport Subreport l nhng bo co con c chn vo bn bo co chnh. Subreport c th khng lin h hoc cc thng s c th c trao qua gia bo co chnh v subreport xc nh ni dung cn hin th . Mail Lable Crystal reports.NET h tr nhng bo co gm nhiu ct v chc nng ny lm cho ta c kh nng to nhng nhn ba th (mail lable). Drill Down Khi nim nm ng sau drill down (xoy su vo chi tit ) l bn cho hin th mt summary (tng kt) trong bn bo co ca bn v ngi s dng c th xoy vo summary xem chi tit hnh thnh tng kt ny th no. 2.3.2 Chn d liu ngun (data source) Hp thoi Standard Report Expert hin th ln nh Hnh 2-05.v mc nh s focus v Tab Data. Khi bn s chn d liu ngun thng qua Tab Data ny, y l bc u tin cho bt k loi expert no. C nhiu cch chn ra d liu ngun, Bng 2-05 sau y s gii thch cc la chn trn khung Available data

source, trong v d ny bn click vo nt Database Files v hp thoi Open m ra ,chn c s d liu xtreme.mdb. Lu : C s d liu xtreme.mdb c sn ti th mc C:\ProgramFiles\Microsoft Visual Studio.NET\Crystal Reports\Samples\Database. Bn bung nhnh Tables ri double click ln bng d liu Customer thm vo khung Tables in Report bn phi. Nhn Next tip tc. Lu : b chn mt bng ra khi khung Tables in Report bn click vo bng sau nhn nt Delete Table bn di khung Tables in Report.

Hnh 2-05: Hp thoi Standard Report Expert Tab Data Bng 2-05: Cc d liu ngun khc nhau

D liu ngun M t Project Data Crystal Reports.NET c th theo .NET Framework v bo co trc tip t dataset xut hin trong ng dng ca bn. Bn c th thy ADO.NET Datasets v Current Connections trong nhnh ny. ADO.NET Datasets cho bit cc lp dataset c lit k trong ca s Project Explorer. Cc lp ny cho php bn uyn chuyn gn kt hu nh vi bt c kiu d liu no. Cn Current Connections cho php bn chn bt c kt ni no hin c thit lp trong ca s Server Explorer OLE DB (ADO) Mc ny cho php to mt connection string truy cp cc d liu ngun s dng mt OLE BD driver bao gm SQL server, Oracle, MS Jet 3.51/4.00 (Access, Excel, Paradox, Dbase , ). Mc ny s cho hin ln hai mn hnh wizard OLE DB Provider v Connection Information yu cu bn cho bit d liu ngun phi kt ni v bt c thng tin no c ngha v v tr v ng nhp. ODBC (RDO) Mc ny cho php to mt connection string truy cp cc d liu ngun s dng mt ODBDC driver cng c 2 mn hnh wizard Datasource Selection v Connection information cho php bn chn mt System DSN hoc file DSN hin hu.Ngoi vic bo co t cc bng d liu, view v stored procedure , Crystal Reports.NET cn cho php bn nhp vo mt lnh SQL dng lm c s cho bn bo co ca bn. Database Files Mc ny cho php bn chn mt trong s nhng dng thc CSDL kiu tp tin bao gm Access, Excel, XML, TTS(loi tp tin nh ngha ca Crystal Field Definition). Favorites Mc ny cho php bn chn ra d liu ngun thng xuyn c dng n m bn a vo danh sch Favorites. Cc item hin hu c th c thm vo bng cch right-click ln chng v chn mc Add to Favorites . Bn cng c th g b mt item khi Favorites bng cch click ln item ri n <F2>. History Mc ny cho php bn chn ra d liu ngun c dng trong cc bn bo co khc trong cng d n. More Data Sources Mc ny bao gm : ADO.NET(XML), access/Excel(DAO) v Field Definition Only. ADO.NET(XML) cho php tm c cc mu tin bng cch khai bo mt li tm v tp tin XML. Bn cng c th chn ra mt ADO.NET datasets hin hu. Cn Access/Excel (ADO) cho php tm c cc mu tin bng cc dng ADO recordset truy cp mt CSDL MS Access hoc Excel spreadsheet. Cn

Field Definition Only ch dng i vi Crystal Field Definition (TTX) mang tnh tng thch li v khng c trin khai mi. 2.3.3 Thit lp mi quan h gia cc bng Nu bc trc bn chn ra cng lc nhiu bn d liu th by gi hp thoi Standard Report Export s hin th Tab Link cho php bn khai bo cc bng d liu ny c nhng mi quan h no, hnh 2-06. Lu : Trong v d ny chng ta ch s dng mt bng customer nn s b qua bc ny.

Hnh 2-06: Hp thoi Standard Report Expert Tab Link 2.3.4 Chn ra cc Field cho bn bo co Bc k tip trn hp thoi Standard Report Export l chn ra cc field s xut hin ln bn bo co. Trn Tab Field (Hnh 2-07), mun s dng field no bn chn field trn khung Available Fieds ri nhn nt Add g chuyn field

chn qua danh sch Field to Display bn phi. tin dng hn, hp thoi Standard Report Export cung cp cho bn hai chc nng Browse Data v Find Field trn Tab Field. Browse Data gip bn xem trc ni dung ca Field m bn chn v nu bn c mt danh sch cc Field rt di th Find Field gip bn tm ra mt Field no trong danh sch . Hnh 2-07: Tab Field dng chn cc Field s dng Cc Field m bn chn ra trn s xut hin theo th t t tri qua phi v mi mu tin trn DataSet s c in ra trn mt hng ca bo co. Bn c th thay i th t cc Field bng cch s dng hai nt mi tn ln xung

trn hp thoi hnh 2-07. Trong v d ny, chng ta chn ra cc Field theo th t: Customer Name, Contact Last Name, Contact Title, E-mail v Last Years Sales. Vi mi Field th s c mt tiu ct tng ng trn bn bo co (mc nh l ly trng tn vi Field), bn c th thay i tiu ct ngay ti bc ny thng qua textbox Column Heading trn hnh 2-07 hoc thay i trc tip ngay trn Report Designer sau ny.

Sau khi chn ra cc Field d liu, bn c th nhn Finish lm vic vi bn bo co trn Report Designer. Tuy nhin, chng ta s qua bc k tip bng vic nhn Next. 2.3.5 Gp nhm v Sp xp (Grouping - Sorting) Ti bc ny, hp thoi Standard Report Export s chuyn sang Tab Group (hnh 2-08), bn c th chn nhng Field lm c s sp xp v tng kt theo nhm bng cch click ln Field mun chn t khung Available Fields sau nhn nt Add g chuyn Field chn qua khung Group By. Trong v d ny, bn chn Group theo Customer.Contry.

Hnh 2-08: Hp thoi Standard Report Expert Tab Group Trn hp thoi hnh 2-08 cng c hai mi tn ln v xung cho php bn thay i th t cc Group nu bo co ca bn c nhiu hn mt Group. Ngoi ra combobox Sort Oder nm bn di khung Group By c bn la chn cho vic sp xp c gii thch theo bng 2-06 di y.

Bng 2-06 Cc la chn sp xp Sort Oder M t Ascending Sp xp theo th t tng dn t A-Z, 1-9, ... Decending Sp xp theo th t gim dn t Z-A, 9-1, ... Original D liu s c th t theo th t ca dataset. Specified Dng to mt custom group cho ring bn [1]. Sau khi chn xong Field dng Group bn nhn Next tip tc. 2.3.6 B sung thng tin tng kt Bn click ln Tab Total trn hp thoi Standard Report Export (hnh 2-09) thm thng tin tng kt (summary) vo bo co. Cc field tng kt (summary field) s cha kt qu ca php tnh ton ph bin nht nh sum, average ... Mun thm mt summary field, bn cn chn ra mt field trn khung Available Fields a vo khung Summaried Fields ( trong v d ny l Customer.Last Years Sales ), sau chn ra kiu summary t hp combo box Summary Type nh trn hnh 2-09.Trong trng hp ny chng ta chn sum v check vo check box Add Grand Totals[2]. C tt c 20 tc t summary kh ph bin m bn c th s dng[3]. Ty theo kiu d liu ca Field bn chn m c th c hoc khng mt vi tc t summary, v d nh bn khng th tnh trung bnh (Average) trn mt Field kiu string. Vi v d ny n y l chng ta c th nhn nt Finish chuyn sang Report Designer nhng gip cc bn hiu r hn cc bc cn li nn chng ta s tip tc kho st tt c cc bc cn li. Bn nhn Next qua bc tip theo. Lu : Bn khng th c bc ny nu bo co ca bn khng c t nht mt Group

Hnh 2-09: Hp thoi Standard Report Expert Tab Total 2.3.7 Cc tnh nng phn tch Ngoi cc summary ph bin ta cn c kh nng thm vo bn bo co mt s chc nng phn tch gip nhn mnh thng tin c th l quang trng. V d, thay v lit k tt c cc nhn vin bn hng trong cng ty bn c th in ra danh sch 10 nhn vin no thc hin doanh thu cao nht ( khen thng) hay c th cho ra 10 nhn vin bn hng t nht trong thng lu h. Trn hp thoi Standard Report Export bn click Tab TopN (hnh 2-10) thm cc chc nng phn tch vo bn bo co. Bn c mt Tab l Customer.Country (nu bn bo co ca bn c bao nhiu Group th s c by nhiu Tab, mi Tab c tn tng ng vi mt Group) v hp combo For this group sort c nm la chn c gii thch theo bng 2-07.

Hnh 2-10: Tab TopN - Sp xp v gp nhm da trn summarized totals. Bng 2-07 Cc la chn trn hp combo For this group sort Cc kiu phn tch M t All Hin th tt c cc gi tr Top N Bn nhp vo mt s N vo text box where N is. Bn bo co s lit k N nhm u tin da trn gi tr summary field theo th t gim dn. Bottom N Bn nhp vo mt s N vo text box where N is. Bn bo co s lit k N nhm cui cng da trn gi tr summary field theo th t tng dn. Top Percentage Bn nhp vo t l N% (0-100) vo text box where Percentage is. Bn bo co s lit k N% nhm u tin da trn gi tr summary field theo th t gim dn. Bottom Percentage

Bn nhp vo t l N% vo text box where Percentage is. Bn bo co s lit k N% nhm cui cng da trn gi tr summary field theo th t tng dn. Vi v d ny bn gi tr All trn hp combo For this group sort, nhn Next tip tc. Lu : Tab TopN ch c th thc hin c khi bn c s thit lp no trn hai Tab Group v Tab Total. 2.3.8 Biu v th (Charting - Graphing) Trn hp thoi Standard Report Export bn c th thm mt s loi Graph khc nhau vo bn bo co thng qua Tab Chart[4] (Hnh 2-11). Bn nhn Next tip tc.

Hnh 2-11: Hp thoi Standard Report Expert Tab Chart 2.3.9 Sng lc thng tin trn bn bo co

Khi in bo co t nhiu bng d liu khc nhau, chc chn l bn khng mun xem tt c mi d liu trn bn bo co. Bn mun chn s dng nhng mu tin no m thi da trn nhng tiu ch no . Tab Select (hnh 2-12) s gip bn thc hin nh ny. C mt s tc t c bn c sn dng cho vic chn lc mu tin c gii thch theo bng 2-08. i vi v d ny, ta khng quan tm vic chn lc mu tin, ngha l tt c cc mu tin s c x l, do ta b qua Tab Select, bn nhn Next qua Tab cui cng.

Hnh 2-12: Tab Select

Bng 2-08: Cc tc t chn lc c bn Tc t chn lc M t Is equal to

So snh bng Is not equal to So snh khng bng Is less than So snh nh hn Is less than or equal to So snh nh hn hoc bng Is greater than So snh ln hn Is greater than or equal to So snh ln hn hoc bng Is one of Thuc mt tp hp nh trc Is between Nm gia Is not between Khng nm gia Is like Ging nh (dng tng t like ca SQL) Is starting with Bt u bi 2.3.10 Chn kiu nh dng cho bo co Tab cui cng trn Standard Report Expert l Tab Style (Hnh 2-13), gip bn chn la format cho bn bo co trc khi hon tt. C tt c 10 style c nh sn m bn c th chn s dng nhung bn khng th thm vo style ring ca bn. Bn c th thm ta (title) ca bn bo co vo Title. Trong trng hp ca chng ta ta ca bo co l International Sales Report, thng tin na s c lu tr trong Summary Infomation ca tp tin bo co v mt Report Title Field s c thm vo Report Designer. Bn nhn nt Finish hon tt vic thit k bo co.

Hnh 2-13: Hp thoi Standard Report Expert Tab Style 2.3.11 Hon tt c bn vic thit k bn bo co Sau khi bn chn Finish trn hp thoi Standard Report Expert, mt tp tin bo co s c to ra vi nhng thit lp m bn va mi thc hin, nh theo hnh 2-14. n y coi nh bn hon tt c bn vic thit k bn bo co.

Hnh 2-14: Tp tin bo co 2.4 Preview bn bo co Mun in ra bn bo co bn thm vo Form1 control CrystalReportViewer trn Toolbox. Khi IDE t ng to ra mt i tng crystalReportViewer1, bn gn thuc tnh Dock ca i tng ny thnh Fill control viewer ny bao ph ton b vng Client trn Form1. ng thi bn thay i thuc tnh WindowState ca Form1 thnh Maximized. Ti hm x l s kin Form_Load bn b sung dng lnh in m sau y: private void Form1_Load(object sender, System.EventArgs e) { crystalReportViewer1.ReportSource = new rptBaocao(); } Lnh trn to mt i tng rptBaocao (l bn bo co ca bn va thit k xong) v gn cho thuc tnh ReportSource ca control viewer. Khi ban cho chy ng dng, n s cho thy mt viewer ca bn bo co nh hnh 2-15.

Hnh 2-15: Bo co InternationalSales ch Preview

Bi 2: Np v hin th Report trong chng trnh .


np v hin th Report trong chng trnh chng ta s tin hnh cc bc nh sau: B1. Thm vo trong d n mt tp DataSet. B2. Thm vo cc bng c cu trc v tn trng ging nh cu trc ca cc d liu cn hin th. B3. Thm vo trong d n mt CrystalReport. Xc nh ngun d liu l DataSet. B4. Thm d n mt Form, v thm vo From mt iu khin crystalReportViewer. B5. Vit Code ly d liu gn cho CrystalReport v hin th Report trong s kin FormLoad. Cc bc trn s c trnh by chi tit di y: 1. Bc 1: Thm vo d n mt DataSet (*.xsd file) (Gi s trong d n ta cn hin th thng tin v cc cun sch c 2 thng tin l Madau v tn u.) - Vo Menu Project\Addnew Item

Thm vo trong d n mt DataSet

2. Bc 2: Thm vo cc bng c cu trc v tn trng ging nh cu trc ca cc d liu cn hin th.


-

R_Click trn nn trng ca DataSet to bng, tip tc to cc ct t tn ct ging vi tn trng d liu cn hin th

3. Bc 3: Thm vo trong d n mt CrystalReport. Xc nh ngun d

liu l DataSet. Vo Menu Project\Add new Item

- Trong bc chn ngun d liu th chn d liu l DataSet1 va mi to trong bc trn trong Project Data\ ADO.NET DataSets. V chn cc bng cn hin th trong DataSet1.

- Nhn Next trong cc bc tip theo nh l phn trnh by trong bi trc. 4. B4. Thm iu khin crystalReportViewer hin th bo co vo

From gi Crystal.

5. Bc 5: Vit Code

ThaoTacDuLieu dl;// Khai bo mt i tng ca lp ThaoTacDuLieu DataSet ds; public Frmbaocao() { // khi to lp ThaoTacDuLieu vi tham s truyn vo l mt chui kt ni. dl = new ThaoTacDuLieu(Program.f.StringConnection); InitializeComponent(); } private void Frmbaocao_Load(object sender, EventArgs e) { // Thi hnh phng thc FillDataSet fill d liu vo DataSet dl.FillDataSet("select * from sach", "sach"); // Ly v i tng DataSet ca lp thao tc d liu. ds = dl.GetDaTaSet; // To ra i tng ca CrystalReport tn l RptBaocao. RptBaocao rp = new RptBaocao(); // Thit lp ngun d liu l DataSet cho CrystalReport. // DataSet truyn vo s truyn d liu cho Tp DataSet to trong bc 1. rp.SetDataSource(ds); rp.Refresh(); // Hin th CrystalReport ln crystalReportViewer bng cch gn i tng Report cho iu khin crystalReportViewer. this.crystalReportViewer1.ReportSource = rp; }

Bi 3: Hin th trng nh ln CrystalReport lc np chng trnh.


hin th trng nh ln Crystalreport ta c th lm theo cc bc sau: 1. Bc 1: Thm vo tp DataSet (*.xsd file) mt bng c mt ct c

kiu trng l mng Byte.

2. Bc 2: Gn ngun d liu hin th cho CrystalReport. Nh bi 1. Hoc R_click trn bo biu

Sau chn bng cn hin th

3. Bc 3: Ko v t trng nh vo v tr cn hin th nh trn bo co.

B4. Vit Code

// Ly nh t trong c s d liu. DataTable dt = dl.FillDataSet("select * from anh", "anh"); // Thng tin v ng dn ca tp nh. string anh = dt.Rows[0][1].ToString(); // To ra mt lung c d liu t File nh. FileStream f = new FileStream(anh, FileMode.Open); BinaryReader br = new BinaryReader(f); DataSet1 datas = new DataSet1(); // Khai bo mt bng c cu trc ging nh cu trc ca bng trong DataSet1. DataTable dt1 = new DataTable("tblImage"); dt1.Columns.Add("name", System.Type.GetType("System.String")); dt1.Columns.Add("anh", System.Type.GetType("System.Byte[]")); // To mt dng mi v gn gi tr cho cc trng. DataRow dr = dt1.NewRow(); dr[0] = anh; // D liu ca trng l mt mng Byte c c t lung c d liu //ca tp nh. dr[1] = br.ReadBytes((int)f.Length); // Thm dng va to vo bng. dt1.Rows.Add(dr); dl.FillDataSet("select * from sach", "sach"); ds = dl.GetDaTaSet; // Thm bng d liu cha nh vo i tng DataSet, m s c s dng lm ngun d liu cho Report ds.Tables.Add(dt1); RptBaocao rp=new RptBaocao (); rp.SetDataSource ( ds); rp.Refresh(); this.crystalReportViewer1.ReportSource = rp;