PDS8475
Active member
- Joined
- Jun 25, 2019
- Messages
- 41
- Programming Experience
- Beginner
Hi I have been trying to work through an old tutorial on printing receipts.
The project has the winforms Form1, frmPrint a class called Receipt.cs and a report called rptReceipt.rdlc
The Receipt class is coded
The winform Form1 is coded
It has the controls
dataGridView which has the datasource receiptBindingSource
txtProductname
txtQuantity
txtPrice
txtTotal
txtCash
btnAdd
btnRemove
btnPrint
frmPrint is coded
frmPrint only has the control
reportViewer
which has the report PrintReceiptDemo.rptReceipt.rdlc selected
Also I had to add the line
otherwise ReceiptBindingSource in frmPrint_Load was not recognised.
The report rptReceipt is set out as
its parameters are pDate, pTotal, pCash, pChange
its data source is PrintReceiptDemo
and data set is called ds which points to the receipt class
the form it self is working and I can add or remove items but when I click print the reportviewer says
A data source instance has not been supplied for the data source 'ds'
I can not figure out what is causing this
even putting a break point on frmPrint where the parameters are passed to the report shows the right data. But it seems as if the report doesn't get the parameters.
Thanks in advance for any help
The project has the winforms Form1, frmPrint a class called Receipt.cs and a report called rptReceipt.rdlc
The Receipt class is coded
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrintReceiptDemo
{
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); } }
}
}
The winform Form1 is coded
C#:
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)
{
txtCash.Text = "0.00";
//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:0.00}", txtCash.Text), string.Format("£{0:0.00}", Convert.ToDouble(txtCash.Text) - total), DateTime.Now.ToString("dd/MM/yyyy")))
{
frm.ShowDialog();
}
}
}
}
It has the controls
dataGridView which has the datasource receiptBindingSource
txtProductname
txtQuantity
txtPrice
txtTotal
txtCash
btnAdd
btnRemove
btnPrint
frmPrint is coded
C#:
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;
BindingSource ReceiptBindingSource = new BindingSource();
//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("pDate",_date),
new Microsoft.Reporting.WinForms.ReportParameter("pTotal",_total),
new Microsoft.Reporting.WinForms.ReportParameter("pCash",_cash),
new Microsoft.Reporting.WinForms.ReportParameter("pChange",_change)
};
this.reportViewer.LocalReport.SetParameters(para);
this.reportViewer.RefreshReport();
}
}
}
frmPrint only has the control
reportViewer
which has the report PrintReceiptDemo.rptReceipt.rdlc selected
Also I had to add the line
BindingSource ReceiptBindingSource = new BindingSource();
otherwise ReceiptBindingSource in frmPrint_Load was not recognised.
The report rptReceipt is set out as
its parameters are pDate, pTotal, pCash, pChange
its data source is PrintReceiptDemo
and data set is called ds which points to the receipt class
the form it self is working and I can add or remove items but when I click print the reportviewer says
A data source instance has not been supplied for the data source 'ds'
I can not figure out what is causing this
even putting a break point on frmPrint where the parameters are passed to the report shows the right data. But it seems as if the report doesn't get the parameters.
Thanks in advance for any help