Ниже приведен код моей формы «Счета клиентов».
Код: Выделить всё
#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);
}
}
Код: Выделить всё
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);
}
}
}
Ниже приведены выходные данные PrintForm
Вывод PrintForm
Ниже приведены выходные данные CustomerBillsForm (он показывает правильные данные).
Вывод CustomerBills
Подробнее здесь: https://stackoverflow.com/questions/792 ... sharp-form