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 1: 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
Step 2: Design your form as below
Name your main form: Form1
Name your print form: frmPrint
Name your report: rptReceipt
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
- How to Create Report Viewer using Stored Procedure in C#
- How to Create QR Code in RDLC Report in C#
- How to Print RDLC Report without Report Viewer in C#
- How to Display an Image in Report Viewer using C#
- Windows Forms: Print Orders/Receipt using Report Viewer in C#
- Windows Forms: How to create a Chart / Graph using RDLC Report in C#
Categories
Popular Posts
Material Admin Dashboard Template
11/15/2024
Responsive Admin Dashboard Template
11/11/2024
Freedash bootstrap lite
11/13/2024
Horizon MUI Admin Dashboard Template
11/18/2024
Modular Admin Template
11/14/2024