How to Print Receipt using Report Viewer in C#

By FoxLearn 12/10/2024 3:21:14 PM   22.03K
To create and print an RDLC report in a Windows Forms application with parameters in C#, follow these steps.

How to Print Receipt using Report Viewer 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 "PrintReceiptDemo" and then click OK

Design your form as shown below.

Name your main form: Form1

print receipt in c#

Name your print form: frmPrint

print receipt in c#

Add the Microsoft.Reporting.WinForms NuGet package by right-clicking your project in Solution Explorer → Manage NuGet Packages.

Search for Microsoft.Reporting.WinForms and install it.

Next, Right-click the project in Solution Explorer, then choose AddNew ItemReporting → Select Report (RDLC).

Name your report: rptReceipt

rdlc receipt in c#

Define the Receipt Class

The Receipt class will serve as the data model to map receipt-related information. It contains properties for Id, ProductName, Price, Quantity, and a calculated Total.

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

Handle the frmPrint Form Logic

The frmPrint form is responsible for rendering the RDLC report and passing the receipt data along with calculated values like total payment, cash received, change, and date to the report viewer.

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;
        // Constructor to pass data to the form
        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 the data source for the RDLC report
            ReceiptBindingSource.DataSource = _list;
            // Pass parameters to the RDLC report
            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();
        }
    }
}

The report viewer uses a ReportDataSource to bind the list of receipts to it. Parameters are passed using the ReportParameter array.

Handle the Form1 (Main Form) Logic

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 entry and add it to the receipt binding source
                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();
            }
        }
    }
}

Main Form (Form1) allows you to add/remove receipts and compute the running total.

When the Print button is clicked, it opens the frmPrint form. The frmPrint_Load method binds data and parameters to the RDLC report for printing.

This example demonstrates how to integrate dynamic data with RDLC reports in a Windows Forms application.

VIDEO TUTORIAL