我设计了一个水晶报告并通过预览和打印对其进行了测试,其中提示了参数值。 在c#on按钮单击事件中使用相同的报告时,会发生Parameterfieldcurrent值异常,表示缺少参数值。 我使用了以下代码,
try
{
ReportDocument r2 = new ReportDocument();
r2.Load(@"C:\\REPORTS\\RptBill.rpt");
r2.SetDatabaseLogon("user", "password", "localhost", "database");
r2.Refresh();
r2.PrintOptions.PrinterName = "printername";
int idx = dataGridView1.CurrentCell.RowIndex;
ParameterFields paramFields = new ParameterFields();
ParameterField pfItemYr = new ParameterField();
pfItemYr.ParameterFieldName = "bill_no";
ParameterDiscreteValue dcItemYr = new ParameterDiscreteValue();
dcItemYr.Value = dataGridView1.Rows[idx].Cells[0].FormattedValue.ToString();
pfItemYr.CurrentValues.Add(dcItemYr);
paramFields.Add(pfItemYr);
r2.PrintToPrinter(1, false,0,0);
}
catch(Exception em)
{
MessageBox.Show("error is: " + em);
}
编辑:作为@Roger建议我使用报告文档的设置参数值,它工作。代码如下,
int idx = dataGridView1.CurrentCell.RowIndex;
string parv = dataGridView1.Rows[idx].Cells[0].FormattedValue.ToString();
ReportDocument r2 = new ReportDocument();
r2.Load(@"C:\\REPORTS\\RptBills.rpt");
r2.SetDatabaseLogon("User", "Password", "localhost", "database");
r2.Refresh();
r2.PrintOptions.PrinterName = printera4name;
r2.SetParameterValue("bill_no",parv);
r2.PrintToPrinter(1, false,0,0);