I created Customer Bills form in C# windows forms apps that has print button. On click of Print button the print form opens that contains the ReportViewer1. The data in RDLC is binded to dataset from SQL server tables. Report Viewer shows this data outside the table.
Below is my Customer Bills form code
#region Print form
// Event handler for Modify button click
private void btnPrint_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow != null) // Ensure a row is selected
{
int invoiceId = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value); // Get the InvoiceId
// Open the PF form and pass the InvoiceId
Print pfForm = new Print
{
InvoiceId = invoiceId // Assuming PF has a public property InvoiceId
};
pfForm.ShowDialog(); // Show PF form as a dialog
}
else
{
MessageBox.Show("Please select a row to modify.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Here is my Print form code
namespace WindowsFormsApp1
{
public partial class Print : Form
{
// Property to receive the InvoiceId from the CustomerBills form
public int InvoiceId { get; set; }
public Print()
{
InitializeComponent();
}
private void Print_Load(object sender, EventArgs e)
{
LoadReport();
}
private void LoadReport()
{
try
{
// Clear existing data sources in the ReportViewer
reportViewer1.LocalReport.DataSources.Clear();
// Fetch the data using the InvoiceId
var customerData = FetchCustomerData();
var invoiceData = FetchInvoiceData();
var combinedInvoiceData = FetchCombinedInvoiceData();
var tInvoiceData = FetchTInvoiceData();
// Add data sources to the ReportViewer
if (customerData.Rows.Count > 0)
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("CustomerDataSet", customerData));
if (invoiceData.Rows.Count > 0)
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("InvoiceDataSet", invoiceData));
if (combinedInvoiceData.Rows.Count > 0)
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("CombinedInvoiceDataSet", combinedInvoiceData));
if (tInvoiceData.Rows.Count > 0)
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("TInvoiceDataSet", tInvoiceData));
// Refresh the report viewer
reportViewer1.RefreshReport();
}
catch (Exception ex)
{
MessageBox.Show($"Error loading report: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private DataTable FetchCustomerData()
{
var adapter = new CustomerTableAdapter();
return adapter.GetData(InvoiceId);
}
private DataTable FetchInvoiceData()
{
var adapter = new InvoiceTableAdapter();
return adapter.GetData(InvoiceId);
}
private DataTable FetchCombinedInvoiceData()
{
var adapter = new CombinedInvoiceTableAdapter();
return adapter.GetData(InvoiceId);
}
private DataTable FetchTInvoiceData()
{
var adapter = new TInvoiceTableAdapter();
return adapter.GetData(InvoiceId);
}
}
}
I want the data in Print form (with ReportViewer) to be displayed inside the table. Below is output of PrintForm PrintForm output
Below is output of CustomerBillsForm(it is showing correct Data) CustomerBills output