Sunday, April 17, 2016

Create Sample Crystal Reports in Visual Studio Step by Step

This tutorial will guide you on how to create Crystal Reports in Visual Studio.


Requirements

1Create empty windows forms application project. Here, you can freely choose either .NET 4.5 or 3.5 project.

2Click view -> toolbox

3Drag CrystalReportViewer to empty area of windows form

The result will be like this

4(This step is for people who creates the .NET 4.5 project in step 1 only) Open app.config

and place the code below into app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

5Right click on your project -> add -> new item

6Select Dataset and click Add.

7xsd file will be created. Right click on empty space -> Add -> DataTable

8The DataTable will be created. Right click on DataTable -> Add -> Column

9You can rename the column as you want. This tutorial will have 2 columns: 'id' and 'name'

10Right click on the project -> Add -> New Item

11Select 'Crystal Reports' item and click Add

12The 'Crystal Reports Gallery' window will show up, select 'Using the Report Wizard' and select 'Standard' option like image below

13Make sure the right pane is empty, then click finish

14You will get the report draft like this

15Right click on empty space -> Page setup

16Check box on 'No Printer' to make tell the report not to stick with specific printer. This option will useful when you want to deploy your application to client which you don't know what printer they use.

17Click OK and Right click on empty space -> Database expert

18On left pane, go to Project data/ADO.NET DataSets/<your project name>/<your DataTable in step 7> , and then click '>' button to make it display on right pane like image below, then click OK

19On file explorer pane, drag fields in DataTable and drop it on Section 3 (Details)

The result will be like this

20Right click on empty space in Report Header -> Insert -> Text Object

21Add any text you want into the text object
22Right click on text object -> Format Object

23Name your text object 'TxtHeader'. Note that you can change the textbox programmatically via this name. (see next step)

24Right click on Form1.cs -> View Code

25Place this code in Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;

namespace CrystalReportSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //generate dummy dataset for report
            DataSet1 ds = new DataSet1();
            ds.Tables["DataTable1"].Rows.Add("test1", "piyapan");
            ds.Tables["DataTable1"].Rows.Add("test2", "xxx");

            //link the report file
            ReportDocument rptDoc = new ReportDocument();
            rptDoc.Load(@"c:\users\esri5034\documents\visual studio 2012\Projects\CrystalReportSample\CrystalReportSample\CrystalReport1.rpt");            
            rptDoc.SetDataSource(ds);

            //edit textbox on report file
            TextObject txt = (rptDoc.ReportDefinition.ReportObjects["TxtHeader"] as TextObject);
            txt.Text = "this is an editing text";

            this.crystalReportViewer1.ReportSource = rptDoc;
            this.crystalReportViewer1.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None;

            this.crystalReportViewer1.EnableDrillDown = false;
            this.crystalReportViewer1.RefreshReport();
            this.crystalReportViewer1.Refresh();
        }
    }
}

26Run your application. Congratulations! you made the first crystal report.



Happy Coding!

No comments:

Post a Comment