Use the data set to display the image .NET program tutorial in the crystal report

zhaozj2021-02-16  51

table of Contents

Description file list Steps form1.csvb.net

description

The C # .NET Windows program demonstrates how to create a dataset and add an image to a dataset, and transfer the data set to the sub-report at runtime.

document list

- bin / debug / canada.jpg- bin / debug / germany.jpg- bin / debug / japan.jpg- bin / debug / usa.jpg- app.ico- assemblyinfo.cs- crystalreport1.cs- crystalReport1.rpt- DynamicImage .csproj- DynamicImage.csproj.user- DynamicImage.sln- Form1.cs- Form1.resx- Readme.txt- Steps.txt

step

* Start a new project / create data set and its model

- New Project - Go to Form1.cs Rear Code - Imports System.Data/System.io - Create Function "CreateData" to create a dataset:

DataSet createData () {DataSet Data = new dataset (); data.tables.add ("images"); data.tables [0] .Columns.add ("country", system.type.gettype ("system.string" )); Data.Tables [0] .Columns.add ("img", system.type.gettype ("system.byte [])); data.writexmlschema (Directory.getCurrentDirectory () " //dynamicimage.xsd ");

- Create a function "createReport" to call CreateData creation data set mode:

Void createreport () {createData ();

- Call CreateReport in the constructor

Public form1 () {/// Required for Windows Form Designer Support // InitializationComponent ();

// // Todo: add any constructor code after initializationComponent call // cretereport ();

- Construct and execute the program / will create DynamicImage.xsd in the Bin / Debug folder.

* Design report

- Project -> Add New Item - Select Crystal Report, and click "Open" - Select "As Blank News" and click OK - Right-click any blank place, select "Database -> Add / Delete Database" Expand ODBC (RDO), select Xtreme Sample Database, and click Finish. - Expand Table, Double-click Customer- Click "OK" - Drag and drop Customer Name and Last Year 'Sales to Details - Right-click any blank place, insert -> Sub-report - Place the sub-report next to Last Year's Sales - Select " Create a sub-report, and name the sub-report to "Flags", click Report Expert - Extend "More Data Source", select ADO.NET (XML) - Find DynamicImage.xsd, and click Finish - Double-click on Images - Click Next, double-click IMG, click Finish - Click "Link" tab - Double-click Country, and click "OK" - Adjust the size of the sub-report - double click on the sub-report, Open sub-report - Remove Reports Header B and Report Footer B - Right-click -> Turk Turns * Back to Code, and Write Crystal Code

- Drag and drop the CrystalReportViewer Control to Form1- Select CrystalReportViewer1, F4 (Properties) - Change the Dock Property, Fill - View Code - Note Off WriteXmlschema (Data Set Data) - Assembled in the createData function Dataset and return it

void AddImageRow (DataTable tbl, string name, string filename) {FileStream fs = new FileStream (filename, FileMode.Open); BinaryReader br = new BinaryReader (fs); DataRow row; row = tbl.NewRow (); row [0] = name; row [1] = br.readbytes ((int) br.basestream.length); tbl.Rows.Add (row); br = null; fs = null;}

DataSet createData () {DataSet Data = new dataset (); data.tables.add ("images"); data.tables [0] .Columns.add ("country", system.type.gettype ("system.string" )); Data.Tables [0] .Columns.add ("img", system.type.gettype ("System.byte [])); //data.writexmlschema (Directory.GetcurrentDirectory () " // DynamicImage .xsd "); addimagerow (data.tables [0]," usa ", directory.getcurrentdirectory () " //usa.jpg "); addimagerow (data.tables [0]," canada ", Directory.getCurrentDirectory ( "//Canada.jpg"); addimagerow (data.tables [0], "germany", Directory.getCurrentDirectory () "//germany.jpg"); addimagerow (data.tables [0], "japan ", Directory.getCurrentDirectory () " //japan.jpg "); return (data);} - Create a report document, pass the data set to the subcare, and bind the report to Crystal News Viewer:

Void createReport () {CrystalReport1 cr = new crystalReport1 (); cr.opensubreport ("flags"). setDataSource (CreateData ()); CrystalReportViewer1.Reportsource = Cr;}

Form1.cs

Using system.drawing; using system.collections; using system.bomponentmodel; using system.data; using system.id;

namespace DynamicImage {///

/// Summary description for Form1 /// public class Form1: System.Windows.Forms.Form {private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1; /// <. Summary> /// Required Designer Variable. /// private system.componentmodel.container component = null; // process: addimagerow // read the image file and add it to the table of the data set //// // [in] TBL Dataset // Country Country // FileName Image File Name // Void AddimageRow (DataTable TBL, String Name, String FileName) {FileStream Fs = New FileStream (FileName, FileMode.Open); // Create a file binaryreader br = new binaryreader (fs); // Create a binary reader DATAROW ROW;

// Create a new data line ROW = TBL.NEWROW ();

/ / Set the country field and image field row [0] = name; row [1] = br.readbytes ((int) br.basestream.length);

// Add the data line to the table TBL.ROWS.ADD (ROW);

/ / Clear BR = null; fs = null;

// Function: createData // Create a dataset, contains a table, the table has two fields: country (string), and IMG (blob / byte []) // Add four records to the table // dataset createData () {dataset Data = new dataset ();

// Add table 'images' to Data Set Data.Tables.Add ("Images"); // Add two fields. TABLES [0] .Columns.add ("country", system.type.gettype. System.String "); Data.Tables [0] .COLUMNS.ADD (" IMG ", System.Type.gettype (" System.byte []));

// Create Data Set Mode (This mode is used for design reports) // After the report is created, the mode files are no longer needed / 4Data.WriteXmlschema (Directory.getCurrentDirectory () "//dynamicimage.xsd");

// Add four lines addimagerow (data.tables [0], "usa", directory.getcurrentdirectory () "//usa.jpg"); addimageRow (data.tables [0], "canada", Directory.getCurrentDirectory ( "//Canada.jpg"); addimagerow (data.tables [0], "germany", Directory.getCurrentDirectory () "//germany.jpg"); addimagerow (data.tables [0], "japan ", Directory.getCurrentDirectory () " //japan.jpg ");

Return (DATA);

// Process: createReport // Create a report and pass the data set // void createReport () {// Create a report CrystalReport1 Cr = New CrystalReport1 ();

// Create a dataset (created by calling function Createdata) to the sub-report "Flags" CR.OpenSubreport ("Flags"). SetDataSource (CreateData ());

// Pass the report document to the viewer CrystalReportViewer1.Reportsource = Cr;}

Public Form1 () {/// Required for Windows form designer support // ionizationComponent (); // // Todo: address (););

VB.NET version

Http://ftp1.businessObjects.com/outgoing/products/devzone/vbnet_win_dynamicimage.zip

转载请注明原文地址:https://www.9cbs.com/read-22434.html

New Post(0)