Windows Forms: Chart / Graph using Crystal Report in C#
By FoxLearn 6/8/2017 7:19:14 PM 6.2K
Create Chart/Graph using Crystal Report in C#
Step 1: Click 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
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
Main form: Form1
Step 4: Design your crystal report as below
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;Password=123@qaz;" 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
- How to Download Crystal Reports for Visual Studio 2010
- Crystal Reports Tutorial
- How to Download and Install Crystal Report Runtime
- How to Download Crystal Reports for Visual Studio 2015 & 2017
- How to Download Crystal Reports for Visual Studio 2022
- How to Download Crystal Reports for Visual Studio 2019
- How to Download Crystal Reports For Visual Studio
- How to Include Crystal Reports Runtime into your application setup