How to Print Invoice in C#
By FoxLearn 12/2/2024 3:14:10 PM 12.09K
DevExpress provides powerful tools for creating custom reports, and you can use XtraReport
to design and generate reports.
How to Print Invoice in C#?
Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "DevReport" and then click OK
Design your form as shown below.
If you have not yet installed DevExpress .NET products, you can view How to download and install DevExpress
Your main form name: Form1
Your print form name: frmPrint
Add a connection string to the App.config
file in your C# project. This connection string will contain the necessary details to connect to your database.
<connectionStrings> <add name="cn" connectionString="Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=123@qaz;" providerName="System.Data.SqlClient"/> </connectionStrings>
We use the Northwind database to play demo. If you haven't got Northwind database, you can view How to download and restore Northwind database in SQL Server
You can install Dapper using the following command in the NuGet Package Manager Console, then create Orders and OrderDetail class to map data return from the northwind database.
Install-Package Dapper
Dapper is a simple Object-Relational Mapper (ORM) that simplifies database interactions in C#.
We will create two classes: Orders
and OrderDetail
, which will map to the corresponding database tables.
public class Orders { public int OrderID { get; set; } public string CustomerID { get; set; } [Display(Name = "Contact Name")] public string ContactName { get; set; } public string Address { get; set; } [Display(Name = "Postal Code")] public string PostalCode { get; set; } public string City { get; set; } public string Phone { get; set; } public DateTime OrderDate { get; set; } }
public class OrderDetail { public int OrderID { get; set; } [Display(Name = "Product Name")] public string ProductName { get; set; } public int Quantity { get; set; } public decimal Discount { get; set; } [Display(Name = "Unit Price")] public decimal UnitPrice { get; set; } public decimal Total { get { return Quantity * UnitPrice - Quantity * UnitPrice * Discount; } } }
We'll design an invoice report to show the order details.
In the frmPrint
form, we will create a method to print the invoice. This method will receive the order and order details as parameters, initialize the report with this data, and display it.
frmPrint
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraEditors; namespace DevReport { public partial class frmPrint : DevExpress.XtraEditors.XtraForm { public frmPrint() { InitializeComponent(); } public void PrintInvoice(Orders order, List<OrderDetail> data) { InvoiceReport report = new InvoiceReport(); //Hide paramater foreach (DevExpress.XtraReports.Parameters.Parameter p in report.Parameters) p.Visible = false; //Init data report report.InitData(order.OrderID.ToString(), order.OrderDate, order.ContactName, order.Address, order.PostalCode, order.City, order.Phone, data); documentViewer1.DocumentSource = report; report.CreateDocument(); } } }
In Form1
, you can load data from the database using Dapper, and then pass the data to the frmPrint
form for printing.
Form1
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using Dapper; using DevExpress.XtraEditors; namespace DevReport { public partial class Form1 : XtraForm { public Form1() { InitializeComponent(); } private void btnPrint_Click(object sender, EventArgs e) { Orders obj = ordersBindingSource.Current as Orders; if (obj != null) { using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString)) { if (db.State == ConnectionState.Closed) db.Open(); // Execute query to get data from Orders and OrderDetails table string query = "select d.OrderID, p.ProductName, d.Quantity, d.Discount, d.UnitPrice from [Order Details] d inner join Products p on d.ProductID = p.ProductID" + $" where d.OrderID = '{obj.OrderID}'"; List<OrderDetail> list = db.Query<OrderDetail>(query, commandType: CommandType.Text).ToList(); // Open print form dialog using (frmPrint frm = new frmPrint()) { frm.PrintInvoice(obj, list); frm.ShowDialog(); } } } } private void btnLoad_Click(object sender, EventArgs e) { using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString)) { if (db.State == ConnectionState.Closed) db.Open(); // Execute query to get data from Orders table string query = "select o.OrderID, c.CustomerID, c.ContactName, c.Address, c.PostalCode, c.City, c.Phone, o.OrderDate" + " from Orders o inner join Customers c on o.CustomerID = c.CustomerID" + $" where o.OrderDate between convert(varchar(25),'{dtFromDate.EditValue}',103) and convert(varchar(25),'{dtToDate.EditValue}',103)"; ordersBindingSource.DataSource = db.Query<Orders>(query, commandType: CommandType.Text); } } } }
VIDEO TUTORIAL