Windows Forms: Print Orders/Receipt using Report Viewer in C#

By FoxLearn 6/21/2017 9:32:54 PM   21.1K
Create rdlc report with Report Viewer, Passing Parameter and Print Orders/Invoice/Receipt in C#

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

print orders c#Step 2: Design your form as below

Name your main form: Form1

c# print orders

Name your print form: frmPrint

c# print invoice

Name your report: rptOrders

c# rdlc 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; }
    public string ContactName { get; set; }
    public string Address { get; set; }
    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; }
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public decimal Discount { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal Total
    {
        get
        {
            return Quantity * UnitPrice - Quantity * UnitPrice * Discount;
        }
    }
}

Step 5: 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.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrintOrders
{
    public partial class frmPrint : Form
    {
        Orders _orders;
        List<OrderDetail> _list;
        public frmPrint(Orders orders, List<OrderDetail> list)
        {
            InitializeComponent();
            _orders = orders;
            _list = list;
        }

        private void frmPrint_Load(object sender, EventArgs e)
        {
            //Init data source
            OrderDetailBindingSource.DataSource = _list;
            //Set parameter for your report
            Microsoft.Reporting.WinForms.ReportParameter[] p = new Microsoft.Reporting.WinForms.ReportParameter[]
            {
                new Microsoft.Reporting.WinForms.ReportParameter("pOrderID",_orders.OrderID.ToString()),
                new Microsoft.Reporting.WinForms.ReportParameter("pOrderDate",_orders.OrderDate.ToString("MM/dd/yyyy")),
                new Microsoft.Reporting.WinForms.ReportParameter("pContactName",_orders.ContactName),
                new Microsoft.Reporting.WinForms.ReportParameter("pPostalCode",_orders.PostalCode),
                new Microsoft.Reporting.WinForms.ReportParameter("pAddress",_orders.Address),
                new Microsoft.Reporting.WinForms.ReportParameter("pCity",_orders.City),
                new Microsoft.Reporting.WinForms.ReportParameter("pPhone",_orders.Phone)
            };
            this.reportViewer.LocalReport.SetParameters(p);
            this.reportViewer.RefreshReport();
        }
    }
}

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 PrintOrders
{
    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();
                //Execute query to get data from orders and customers data
                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 '{dtFromDate.Value}' and '{dtToDate.Value}'";
                ordersBindingSource.DataSource = db.Query<Orders>(query, commandType: CommandType.Text);          
            }
        }

        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 orderdetails and products data
                    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();
                    using(frmPrint frm = new frmPrint(obj, list))
                    {
                        frm.ShowDialog();
                    }
                }
            }
        }
    }
}

VIDEO TUTORIALS