Windows Forms: Print Receipt using Report Viewer in C#

By FoxLearn 5/26/2017 10:01:36 PM   21.82K
Create receipt report, passing parameters, print RDLC 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 "PrintReceiptDemo" and then click OK

print receipt in c#Step 2: Design your form as below

Name your main form: Form1

print receipt in c#

Name your print form: frmPrint

print receipt in c#

Name your report: rptReceipt

rdlc receipt in c#

Step 3: You need to create a Receipt class to map data

public class Receipt
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }
    public string Total { get { return string.Format("{0}$", Price * Quantity); } }
}

Add code to handle frmPrint

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PrintReceiptDemo
{
    public partial class frmPrint : Form
    {
        List<Receipt> _list;
        string _total, _cash, _change, _date;
        //You can pass data by using constructor
        public frmPrint(List<Receipt> dataSource, string total, string cash, string change, string date)
        {
            InitializeComponent();
            _list = dataSource;
            _total = total;
            _cash = cash;
            _change = change;
            _date = date;
        }

        private void frmPrint_Load(object sender, EventArgs e)
        {
            //Set datasource, parameters to RDLC report
            ReceiptBindingSource.DataSource = _list;
            Microsoft.Reporting.WinForms.ReportParameter[] para = new Microsoft.Reporting.WinForms.ReportParameter[]
            {
                new Microsoft.Reporting.WinForms.ReportParameter("pTotal",_total),
                new Microsoft.Reporting.WinForms.ReportParameter("pCash",_cash),
                new Microsoft.Reporting.WinForms.ReportParameter("pChange",_change),
                new Microsoft.Reporting.WinForms.ReportParameter("pDate",_date)
            };
            this.reportViewer.LocalReport.SetParameters(para);
            this.reportViewer.RefreshReport();
        }
    }
}

Add code to handle Form1

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PrintReceiptDemo
{
    public partial class Form1 : Form
    {
        int order = 1;
        double total = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtProductName.Text) && !string.IsNullOrEmpty(txtPrice.Text))
            {
                //Create a new receipt, then add to binding souce
                Receipt obj = new Receipt() { Id = order++, ProductName = txtProductName.Text, Price = Convert.ToDouble(txtPrice.Text), Quantity = Convert.ToInt32(txtQuantity.Text) };
                total += obj.Price * obj.Quantity;
                receiptBindingSource.Add(obj);
                receiptBindingSource.MoveLast();
                txtProductName.Text = string.Empty;
                txtPrice.Text = string.Empty;
                txtTotal.Text = string.Format("{0}$", total);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Init empty list
            receiptBindingSource.DataSource = new List<Receipt>();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            //Get current object, then remove from binding source
            Receipt obj = receiptBindingSource.Current as Receipt;
            if (obj != null)
            {
                total -= obj.Price * obj.Quantity;
                txtTotal.Text= string.Format("{0}$", total);
            }
            receiptBindingSource.RemoveCurrent();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //Open print form
            using (frmPrint frm = new frmPrint(receiptBindingSource.DataSource as List<Receipt>, string.Format("{0}$", total), string.Format("{0}$", txtCash.Text), string.Format("{0:0.00}$", Convert.ToDouble(txtCash.Text) - total), DateTime.Now.ToString("MM/dd/yyyy")))
            {
                frm.ShowDialog();
            }
        }
    }
}

VIDEO TUTORIALS