如何在报表上显示传递给查询的参数

时间:2014-08-18 10:35:15

标签: c# .net winforms parameter-passing reportviewer

在我的.NET winforms应用程序中,我使用ReportViewer。我只需创建DataSet并配置DataAdapter,然后使用report.rdlc中的DataSet来设计报告。最后,我将该报告绑定到报告查看器。

这是报告背后的查询

SELECT reference AS Month, payment_date AS Date, SUM(total_records) AS Records, SUM(total_amount) AS Amount, ROUND(SUM(total_amount) / SUM(total_records), 2) AS Average
FROM Payment
GROUP BY reference, payment_date
HAVING (payment_date BETWEEN @fromDate AND @toDate)

然后我将参数传递给适配器并按如下方式显示报告......

    private void btnShowReport_Click(object sender, EventArgs e)
    {
        this.PaymentTableAdapter.Fill(this.DataSetWagesPaymentSummary.Payment,dtpFrom.Value.ToString(),dtpTo.Value.ToString());

        this.rpWagesSummary.RefreshReport();
    }

[dtpFrom和dtpTo是DateTimePickers]

现在我尝试做的只是想在报告中显示这些@toDate@fromDate参数,就像这样......


PAYMENT SUMMARY REPORT 
FROM 1/1/2014 TO 31/1/2014

1 个答案:

答案 0 :(得分:2)

您需要在报告中创建一个参数:

  1. 报告数据 - 参数
  2. 右键单击 - 添加新参数,例如toDate。
  3. 接下来,您需要将参数传递给报告。

    ReportParameter rp1 = new ReportParameter("toDate", dtpFrom.Value.ToString());
    this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp1 });
    

    然后在报告中添加一个文本框并设置表达式。

    例如:

    =Parameters!toDate.Value