使用RDLC报表(三)--向RDLC报表传入参数
在使用报表向客户展示结果数据时,实时的在报表中显示某些特定的数据是必需的,如:显示的部门、打印的日期等。本文只简单的演示向报表内传入一个字符值。如有其它问题,欢迎讨论。
1、新建一个工程TestReport,一个Form窗体,放入一个TextBox、一个Button按钮,再放入一个ReportViewer控件。
2、在ReportViewer上选择新建一个报表
3、在打开的报表设计器中,选择工具栏的“报表”中的“报表参数”,新加一个参数,名称为content,数据类型为string,确定。
4、在报表设计器的页面上放入一个文本框,在文本框上按鼠标右键->属性,在“文本框属性”窗口中,选择“常规”选项卡内下部的“值”后面的“编辑表达式”按钮(就是那个Fx),在此窗口内,左下框内选择参数,在右下框将会出现在上一步中设置的参数,双击此参数,在上面的框内将出现所需要的表达式:=Parameters!content.Value。保存此报表。报表默认名称为Report1.rdlc。
5、在Form窗体内双击按钮,编写如下代码:
program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
namespace Rdlc_Report
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
namespace Rdlc_Report
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
reportViewer1.LocalReport.ReportEmbeddedResource = "Report1.rdlc";
reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
//reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1_rusp_report_chat", DB.GetTable("select * from rusp_report_chat order by id desc")));
this.reportViewer1.RefreshReport();
}
{
reportViewer1.LocalReport.ReportEmbeddedResource = "Report1.rdlc";
reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
//reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1_rusp_report_chat", DB.GetTable("select * from rusp_report_chat order by id desc")));
this.reportViewer1.RefreshReport();
}
private void button1_Click(object sender, EventArgs e)
{
{
reportViewer1.LocalReport.ReportEmbeddedResource = "Report1.rdlc";
ReportParameter rp = new ReportParameter("content", this.textBox1.Text);
this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });
this.reportViewer1.RefreshReport();
}
}
}
ReportParameter rp = new ReportParameter("content", this.textBox1.Text);
this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });
this.reportViewer1.RefreshReport();
}
}
}