Windows Forms: Chart / Graph using Crystal Report in C#

Create Chart/Graph using Crystal Report in C#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "CrystalChart" and then click OK

crystal report c#

Step 2: Create an OrderReport to map data as below

public class OrderReport
{
    public string Year { get; set; }
    public int Number { get; set; }
}

Step 3: Design your form as below

Print form: frmPrint

crystal report

Main form: Form1

crystal report c#

Step 4: Design your crystal report as below

crystal report

If you don't have Crystal Report for Visual Studio you can view How to download and install Crystal Report for Visual studio

Step 5: Add code to handle your form

Add a connection string to the App.config file

<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <connectionStrings>
    <add name="cn" connectionString="Data Source=.;Initial Catalog=Northwind;User ID=sa;[email protected];" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

Right click on your project select Manage NuGet Packages -> Search dapper -> Install

We use the Northwind database to play demo. You can view How to download and restore Northwind database to SQL Server

Add code to handle frmPrint as below

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;

namespace CrystalChart
{
    public partial class frmPrint : Form
    {
        List<OrderReport> _list;
        //Passing data to constructor
        public frmPrint(List<OrderReport> list)
        {
            InitializeComponent();
            _list = list;
        }

        private void frmPrint_Load(object sender, EventArgs e)
        {
            //Set data source to report
            rptChart1.SetDataSource(_list);
            crystalReportViewer1.ReportSource = rptChart1;
        }
    }
}

Form1

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 Dapper;
using System.Configuration;
using System.Data.SqlClient;

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

        private void btnLoad_Click(object sender, EventArgs e)
        {
            using(IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
            {
                if (db.State == ConnectionState.Closed)
                    db.Open();
                string query = "select x.[Year], count(*) as Number from" +
                                 " (select OrderID, YEAR(OrderDate) as [Year] from Orders) x" +
                                 " group by x.Year" +
                                 " order by x.Year asc";
                //Execute query, then set data to data source
                List<OrderReport> list = db.Query<OrderReport>(query, commandType: CommandType.Text).ToList();
                orderReportBindingSource.DataSource = list;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //Passing data to constructor, then open print form
            using(frmPrint frm = new frmPrint(orderReportBindingSource.DataSource as List<OrderReport>))
            {
                frm.ShowDialog();
            }
        }
    }
}

VIDEO TUTORIALS