U Visual studio 2008 built-in crystal report 10
Simple Application in u asp.net website
U The purpose of this example is to allow friends who are new to the Crystal Report to master their applications. Therefore, the example is simple, concise, and clear, and does not connect to the background database.
Visual studio 2008 create an asp.net website CrystalReportsDemo
Right-click the project --> Add reference:
Add an object class DemoModel. cs
This website does not adopt any development mode, so the class code should be placed in the App_Code folder of asp.net. You can create this folder in the project or create a new class according to the system prompt when adding the class:
The content of DemoModel. cs is as follows:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
/// <Summary>
/// DemoModel an object
/// </Summary>
Public class DemoModel
{
Public DemoModel ()
{}
Public DemoModel (string id, string name)
{
This. id = id;
This. name = name;
}
Private string id; // Encoding
Public string Id
{
Get {return id ;}
Set {id = value ;}
}
Private string name; // name
Public string Name
{
Get {return name ;}
Set {name = value ;}
}
}
Right-click Project --> Add new item --> Crystal REPORT
After the name is "CrystalReport. rpt" is determined, the following prompt is displayed automatically:
There are three single-choice options for creating a new Crystal Report document. There are three options for selecting experts:
You can select an empty report and right-click the open report --> report expert (with the same effect). By default, the Report Wizard is used. select experts as the standard (usually used) and click OK.
Project Data -->. Net object
The added object DemoModel. cs is of course only a data source.
Note: The above Entity class and the crystal report file can be found in the same assembly. When the website adopts the N-layer mode for development, the entity class is another assembly, which cannot be found here, other data sources can be used
Add DemoModel to the right, as shown in:
Click Next to set the fields to be displayed in the report.
Define the report format here and click Finish.
The report header, header, details, end of report, and footer are displayed on the left side of the page to facilitate the design of the report format. You can right-click the corresponding area to set the report format, after applying the crystal report, you can try it. Here we only set the details area in a simple way.
From the right, field Resource Manager
Database field. Find the added data source DemoModel. You can view the object attributes and drag the fields to the details area on the left.
Of course, the display style can be defined by yourself. You can try it and there are many operations by right-clicking
Note: No variables are involved in this report. Therefore, the report is designed and saved.
Close the report window.
The designed crystal report is displayed in Default. aspx.
Drag a control from the report bar of the toolbox to display the crystal report.
CrystalReportViewer can not be used for any other work,
The Code on the Default. aspx page is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<% @ Register Assembly = "CrystalDecisions. Web, Version = 10.5.3700.0, Culture = neutral, PublicKeyToken = 692fbea5521e1304"
Namespace = "CrystalDecisions. Web" TagPrefix = "CR" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<CR: CrystalReportViewer ID = "CrystalReportViewer1" runat = "server" AutoDataBind = "true"/>
</Div>
</Form>
</Body>
</Html>
The content of the Default. aspx. cs post-page class is as follows:
Using System;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
// Introduce the namespace
// List is used and has a generic
Using System. Collections;
Using System. Collections. Generic;
Using CrystalDecisions. Shared;
Using CrystalDecisions. CrystalReports. Engine;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Create a report document
ReportDocument myReport = new ReportDocument ();
// Obtain the physical path of the report file
String reportPath = Server. MapPath ("~ /CrystalReport. rpt ");
// Load the report file
MyReport. Load (reportPath );
//************************************** *************************
/* Because the database is not connected, we add a data source for the display effect.
The object is used in the report. Here we manually add the following objects:
And load a List set. Of course, if you connect to the database later, it will also be used to modify the data source.
Others do not need to be changed too much */
DemoModel dm1 = new DemoModel ("10001", "demo1 ");
DemoModel dm2 = new DemoModel ("10002", "demo2 ");
DemoModel dm3 = new DemoModel ("10003", "demo3 ");
DemoModel dm4 = new DemoModel ("10004", "demo4 ");
List <DemoModel> dmList = new List <DemoModel> ();
DmList. Add (dm1 );
DmList. Add (dm2 );
DmList. Add (dm3 );
DmList. Add (dm4 );
// The data source object has been added.
//************************************** ***************************
// Set the data source for the new report document
MyReport. SetDataSource (dmList );
// Bind the created report document
This. CrystalReportViewer1.ReportSource = myReport;
This. CrystalReportViewer1.DataBind ();
}
}