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 New Visual C # project Windows ApplicationProgram, Set form1 text = "", startposition = centerscreen // The program starts to appear in the center of the screen, and keep other attributes by 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 Files \ Microsoft Visual Studio. Net \ Crystal Reports \ samples \ reports \ feature examples \ chart. rpt