VC # .NET browsing Crystal Report (from Abao's column)

xiaoxiao2021-03-06  58

Deding Crystal Reports (Crystal Report) Embed a standard report tool for Microsoft Visual Studio .NET, and upgrade to Crystal Reports for Visual Studio

.NET. Its advantages are: 1, in

The .NET platform can easily create an interactive, high-quality report content, which is the main advantage of Crystal Reports; 2. Crystal Reports for Visual Studio

.NET, you can carry the report on the web platform and the Windows platform, and publish the Crystal report as a report web service in the web server; in

In .NET, the chart is actually a Crystal report that interacts with other controls in the application. Here I introduce you how to browse the crystal report in the Windows Form Application.

Design steps:

1. Software Environment: Requires the system installed Visual Studio

.NET integration development system, only

.NET FRAMEWORK SDK cannot achieve this effect, become

.NET Framework SDK has no crystal report control.

2. Newly built a Visual C # item Windows application, set the text = "Crystal Report Browser", startPosition = cENTERSCREEN / / Program starts to appear in the center, and other properties remain default;

3, drag into a CrystalReportViewer from the toolbox, a button, an OpenFiledialog, to the form.

The form layout of the entire designer simply puts Button1 in the middle of the form bottom, no longer need additional layout.

Set the properties of CrystalReportViewer1 as follows:

This.CrystalReportViewer1.dock = system.windows.forms.dockstyle.Fill;

// Stop mode is full of full form

// Expand Dockpadding

this.crystalReportViewer1.dockpadding.bottom = 50;

// The bottom empty is placed in the area of ​​Button

This.crystalReportViewer1.dockpadding.Left = 5;

This.CrystalReportViewer1.dockpadding.right = 5;

this.crystalReportViewer1.dockpadding.top = 5;

THIS.CRYSTALREPORTVIEWER1.REPORTSOURCE = NULL;

// Do not load the report resource

Set the properties of Button1 as follows:

This.Button1.Anchor = system.windows.Forms.Anchorstyles.bottom

/ / Keep fixed with the lower side of the form

This.button1.text = "Open Report";

OpenFileDialog1 is the control to open the file and set its properties as follows:

this.openfiledialog1.filter

= "Crystal Report (* .rpt) | * .RPT | All files (*. *) | *. *";

/ / Provide the file type of the open file dialog,

The default type is the type of the most defined default of this string

this.openfiledialog1.title = "Open Crystal Report"; // Open the title of the file dialog

The layout renderings are as follows:

4, double-click Button1, add button1_click click event:

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Try

{

IF (OpenFileDialog1.Showdialog () == DialogResult.ok)

This.CrystalReportViewer1.reportsource = @ OpenFiledialog1.FileName

// Load the crystal report to bind the report file to the CrystalReportView control;

}

Catch (Exception Error)

{

MessageBox.show (Error.Tostring (), "Error");

}

}

5, ok! Press CTRL F5 to run.

You can browse the existing report instances in your system:

... / Program Files / Microsoft Visual Studio

.NET / CRYSTAL REPORTS / SAMPLES / Reports / Feature Examples / Chart.rpt

The entire source code code is as follows:

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Namespace WindowsApplication10

{

///

/// Form1 summary description.

///

Public Class Form1: System.Windows.Forms.form

{

Private crystaldecisions.windows.Forms.crystalReportViewer CrystalReportViewer1;

Private system.windows.Forms.Button button1;

Private system.windows.forms.openfiledialog openfiledialog1;

///

/// The required designer variable.

///

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

//

// Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

///

/// Clean all the resources being used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent () {

this.crystalReportViewer1 = New

Crystaldecisions.windows.Forms.crystalReportViewer ();

This.button1 = new system.windows.Forms.Button ();

This.openfileDialog1 = new system.windows.Forms.openFiledialog ();

THIS.SUSPENDLAYOUT ();

//

// CrystalReportViewer1

//

This.CrystalReportViewer1.activeViewIndex = -1;

this.crystalReportViewer1.dock

= System.windows.Forms.dockStyle.Fill;

this.crystalReportViewer1.dockpadding.bottom = 50;

This.crystalReportViewer1.dockpadding.Left = 5;

This.CrystalReportViewer1.dockpadding.right = 5;

this.crystalReportViewer1.dockpadding.top = 5;

This.crystalReportViewer1.name = "CrystalReportViewer1";

THIS.CRYSTALREPORTVIEWER1.REPORTSOURCE = NULL;

This.crystalReportViewer1.size = new system.drawing.size (292, 273);

This.crystalReportViewer1.tabindex = 0;

//

// Button1

//

This.Button1.Anchor = system.windows.Forms.Anchorstyles.bottom

This.Button1.Location = new system.drawing.point (104, 240);

This.button1.name = "button1";

This.button1.tabindex = 1;

This.button1.text = "Open Report";

This.Button1.click = new system.eventhandler (this.button1_click);

//

// OpenFileDialog1

//

This.openfileDialog1.filter = "Crystal Report (* .rpt) | * .rpt | All files (*. *) | *. *";

THIS.OpenFileDialog1.title = "Open Crystal Report";

//

// Form1

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

THIS.CLIENTSIZE = New System.drawing.size (292, 273);

this.controls.addrange (new system.windows.forms.control] {

this.button1,

THIS.CRYSTALREPORTVIEWER1});

THIS.NAME = "Form1";

This.StartPosition = system.windows.forms.formstartPosition.CenterScreen; this.text = "Crystal Report Browser";

This.ResumeLayout (false);

}

#ndregion

///

/// The main entry point for the application.

///

[Stathread]

Static void main ()

{

Application.run (New Form1 ());

}

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Try

{

IF (OpenFileDialog1.Showdialog () == DialogResult.ok)

This.CrystalReportViewer1.reportsource = @ OpenFiledialog1.FileName

/ / Load the crystal report to bind the resource report to Crystal News Viewer

}

Catch (Exception Error)

{

Messagebox.show (Error.Tostring (), "Error"); // Handling an exception error

}

}

}

}

In fact, there are many ways to bind reports to the CrystalReportViewer control.

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

New Post(0)