Vc#. NET Browse Crystal Reports

Source: Internet
Author: User
Tags filter bind tostring visual studio
The famous Crystal Reports (Crystal Report) has been embedded in Microsoft Visual Studio. NET, while upgrading to Crystal Reports for Visual Studio. Net. Its advantages are: 1, in the. NET platform can easily create interactive, high-quality presentation of the content of the report, which is Crystal Reports has long been the main advantage; 2, using Crystal Reports for Visual Studio. NET, can be found in the We Platform B and Windows platform, and publish the Crystal report as a report Web service on the Web server, 3, using the Web application created by Crystal, allows users to drill down and filter information as needed. In. NET, a chart is actually a Crystal report that interacts with other controls in your application. Here I show you how to browse the Crystal Report in a Windows forms application.

Design steps:

1. Software environment: Requires the system to have the visual Studio. NET integrated development system installed, only the. NET Framework SDK does not implement this example, and becomes a. NET Framework SDK without a crystal report control.

2. Create a new Visual C # project Windows application, set up Form1 text= "Crystal Report Browser", Startposition=centerscreen//program began to appear in the center of the screen, other properties are maintained by default;

3. Drag a crystalreportviewer from the toolbox, a button, a OpenFileDialog, to the form.

The entire designer's form layout simply places the button1 in the middle of the bottom of the form, and no more redundant layouts are needed.

Set the CrystalReportViewer1 properties as follows:

This.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
Dock mode to fill the entire form
Expand +dockpadding
This.crystalReportViewer1.DockPadding.Bottom = 50;
Bottom vacated area where button is placed
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 Button1 properties as follows:

This.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
Keep the bottom of the form fixed
This.button1.Text = "Open report";



OpenFileDialog1 is the control that opens the file, setting its properties as follows:

This.openFileDialog1.Filter
= "Crystal (*.rpt) |*.rpt| all Documents (*.*) |*.*";
Provides the file type for the Open File dialog box.
The default type is the first defined type of this string
This.openFileDialog1.Title = "Open Crystal Report";
Open the title of the file dialog box

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 and bind the report file to the Crystalreportview control;
}
catch (Exception error)
{
MessageBox.Show (Error. ToString (), "error");
}
}



5, ok! Press CTRL+F5 to run it.

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 is as follows:

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Namespace WindowsApplication10
{
<summary>
Summary description of the 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>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: Add any constructor code after the InitializeComponent call
//
}
<summary>
Clean up all resources that are 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>
Designer supports required methods-do not use the Code editor to modify
The contents 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.button1_click);
//
OpenFileDialog1
//
This.openFileDialog1.Filter = "Crystal (*.rpt) |*.rpt| all Documents (*.*) |*.*";
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);
}
#endregion
<summary>
The main entry point for the application.
</summary>
[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 Crystal Report, bind resource report to Crystal Report Viewer
}
catch (Exception error)
{
MessageBox.Show (Error. ToString (), "error"); Handling Exception Errors
}
}
}
}



In fact, there are many ways to bind a report to a Crystalreportviewer control.


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.