View Crystal Report in VC #. Net

Source: Internet
Author: User

The well-known Crystal Reports (Crystal Reports) has been embedded with Microsoft Visual Studio. NET standard reporting tools and upgraded to Crystal Reports for Visual Studio. NET. It has the following advantages: 1. NET platform can easily create interactive and high-quality Reports, which is also a long-term advantage of Crystal Reports; 2. Use Crystal Reports for Visual Studio. NET, which can carry reports on the Web platform and Windows platform, and publish the Crystal Report as a Report Web service on the Web server. 3. Use the Web application created by Crystal Report, this allows you to deepen charts and filter information as needed. In. NET, a chart is actually a Crystal report that interacts with other controls in the application. Here I will show you how to browse Crystal Reports in Windows Forms applications.
Design steps:

1. software environment: the system must have Visual Studio. Net integrated development system installed. Only the. Net Framework SDk is installed. This example cannot be implemented. Net Framework SDK does not have a crystal report control.

2. Create a Visual C # project Windows application, set Form1's Text = "", StartPosition = CenterScreen // The program starts to appear in the center of the screen, and keep other properties as default;

3. Drag a CrystalReportViewer, a Button, and an openFileDialog from the toolbox to the form.

The form layout of the whole designer only needs to place button1 in the middle of the bottom side of the form, and no additional layout is required.

Set the properties of crystalReportViewer1 as follows:
This. crystalReportViewer1.Dock = System. Windows. Forms. DockStyle. Fill;
// The docking mode is full of the entire form
// Expand + DockPadding
This. crystalReportViewer1.DockPadding. Bottom = 50;
// The Button is left blank at the bottom.
This. crystalReportViewer1.DockPadding. Left = 5;
This. crystalReportViewer1.DockPadding. Right = 5;
This. crystalReportViewer1.DockPadding. Top = 5;
This. crystalReportViewer1.ReportSource = null;
// Do not load report resources first
Set the attributes of button1 as follows:

This. button1.Anchor = System. Windows. Forms. AnchorStyles. Bottom;
// Fixed to the bottom of the form
This. button1.Text = "Open Report ";



OpenFileDialog1 is the control for opening files. Its Attributes are set as follows:

This. openFileDialog1.Filter
= "Crystal Report (*. rpt) | *. rpt | all files (*. *) | *.*";
// Provides the file type in the open file dialog box,
The default type is the type defined before this string.
This. openFileDialog1.Title = "open a crystal Report ";
// The title of the open file dialog box


The layout is as follows:





4. Double-click button1 to add a button#click event:

Private void button#click (object sender, System. EventArgs e)
{
Try
{
If (openFileDialog1.ShowDialog () = DialogResult. OK)
This. crystalReportViewer1.ReportSource = @ openFileDialog1.FileName;
// Load the Crystal Report and 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 view existing report instances in your system:

... Program FilesMicrosoft Visual Studio. NETCrystal ReportsSamplesReportsFeature ExamplesChart. rpt






Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;
Namespace WindowsApplication10
{
/// <Summary>
/// Summary of Form1.
/// </Summary>
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;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;
Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
//
// TODO: add Any constructor code after InitializeComponent calls
//
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region Windows Form Designer generated code
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
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. button#click );
//
// OpenFileDialog1
//
This. openFileDialog1.Filter = "Crystal Report (*. rpt) | *. rpt | all files (*. *) | *.*";
This. openFileDialog1.Title = "open a 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 );
}
# Endregion
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. Run (new Form1 ());
}
Private void button#click (object sender, System. EventArgs e)
{
Try
{
If (openFileDialog1.ShowDialog () = DialogResult. OK)
This. crystalReportViewer1.ReportSource = @ openFileDialog1.FileName;
// Load the Crystal Report and bind the resource report to the crystal report viewer.
}
Catch (Exception error)
{
MessageBox. Show (error. ToString (), "error"); // handle exception errors
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.