DevExpress: Print Invoice in C#

How to Create an invoice/receipt/orders using DevExpress Report in C#, Getting Started with DevExpress XtraReport

Step 1Click 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

devexpress reportStep 2: Design your form as below

If you have not yet installed DevExpress .NET products, you can view How to download and install DevExpress

Your main form name: Form1

devexpress report

Your print form name: frmPrint

devexpress report

Step 3: Add a connection string to the App.config file

  <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

Step 4: Install Dapper from nuget, then create Orders and OrderDetail class to map data return from the northwind database

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;
        }
    }
}

Step 5: Design your report as below

devexpress xtrareport

Step 6: Add code to handle your forms as below

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();
        }
    }
}

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();
                //Excute 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 TUTORIALS