RDLC子报表

一个报表,显示主记录与相关的子记录,使用RDLC,经两个多小时的摸索,总算是搞出个DEMO,赶紧记下来,以备以后查看。

步骤1:建立一个报表,在报表中加入“列表”控件,在列表中加入文本框,将主记录数据源绑定到文本框中,示例中有一个“姓名”字段,主表与子表靠这个字段来关联。

步骤2:在列表中插入一个“子报表”控件,新建一个报表,将此子报表控件的子报表属性选择为新建的这个报表。

步骤3:在子报表属性的参数中加入一个参数“姓名”,参数值=“=Fields!姓名.Value”。

步骤4:打开子报表,在报表属性中加入一个报表参数“姓名”,这个参数与上一步设置的参数同名。

步骤5:在子报表中加入表格控件,表的属性中的筛选器加入“=Fields!姓名.Value”=“=Parameters!姓名.Value”。

步骤6:在报表浏览器所在的界面中还需有代码配合,我的示例代码如下:

view plaincopy to clipboardprint?
public partial class Form2 : Form  
    {  
        DataTable dt2;  
          
        public Form2()  
        {  
            InitializeComponent();  
              
        }  
 
        private void Form2_Load(object sender, EventArgs e)  
        {  
            ReportViewer reportViewer = new ReportViewer();  
            reportViewer.ProcessingMode = ProcessingMode.Local;  
            reportViewer.LocalReport.ReportPath = "Report1.rdlc";  
 
            reportViewer.LocalReport.SubreportProcessing += new Microsoft.Reporting.WinForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);  
 
            reportViewer.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1_DataTable1", loadData1()));  
 
            reportViewer.Dock = DockStyle.Fill;  
            this.Controls.Add(reportViewer);  
 
            reportViewer.RefreshReport();  
     
        }  
 
        
 
        private void LocalReport_SubreportProcessing(object sender, Microsoft.Reporting.WinForms.SubreportProcessingEventArgs e)  
        {  
            if (dt2 == null)  
                dt2 = loadData2();  
 
            e.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1_DataTable2", dt2));  
        }  
 
 
        private DataTable loadData1()  
        {  
            DataSet1.DataTable1DataTable dt = new DataSet1.DataTable1DataTable();  
 
            DataRow dr1 = dt.NewRow();  
            dr1["姓名"] = "王三";  
            dr1["日期"] = DateTime.Now.ToShortDateString();  
            dr1["金额"] = 130.45;  
            dr1["余额"] = 1234.33;  
            dt.Rows.Add(dr1);  
 
            DataRow dr2 = dt.NewRow();  
            dr2["姓名"] = "李四";  
            dr2["日期"] = DateTime.Now.ToShortDateString();  
            dr2["金额"] = 111.33;  
            dr2["余额"] = 22.3;  
            dt.Rows.Add(dr2);  
 
            return dt;  
        }  
 
 
        private DataTable loadData2()  
        {  
            DataSet1.DataTable2DataTable dt2 = new DataSet1.DataTable2DataTable();  
            DataRow d1 = dt2.NewRow();  
            d1["姓名"] = "王三";  
            d1["编号"] = "0001";  
            d1["名称"] = "床位费";  
            d1["价格"] = 10;  
            d1["数量"] = 1;  
            d1["金额"] = 10;  
            dt2.Rows.Add(d1);  
 
            DataRow d2 = dt2.NewRow();  
            d2["姓名"] = "王三";  
            d2["编号"] = "0002";  
            d2["名称"] = "护理费";  
            d2["价格"] = 8;  
            d2["数量"] = 3;  
            d2["金额"] = 24;  
            dt2.Rows.Add(d2);  
 
            DataRow d3 = dt2.NewRow();  
            d3["姓名"] = "李四";  
            d3["编号"] = "0002";  
            d3["名称"] = "注射费";  
            d3["价格"] = 8;  
            d3["数量"] = 3;  
            d3["金额"] = 24;  
            dt2.Rows.Add(d3);  
 
            return dt2;  
          
        } 
public partial class Form2 : Form
    {
        DataTable dt2;
       
        public Form2()
        {
            InitializeComponent();
           
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            ReportViewer reportViewer = new ReportViewer();
            reportViewer.ProcessingMode = ProcessingMode.Local;
            reportViewer.LocalReport.ReportPath = "Report1.rdlc";

            reportViewer.LocalReport.SubreportProcessing += new Microsoft.Reporting.WinForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

            reportViewer.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1_DataTable1", loadData1()));

            reportViewer.Dock = DockStyle.Fill;
            this.Controls.Add(reportViewer);

            reportViewer.RefreshReport();
  
        }

     

        private void LocalReport_SubreportProcessing(object sender, Microsoft.Reporting.WinForms.SubreportProcessingEventArgs e)
        {
            if (dt2 == null)
                dt2 = loadData2();

            e.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1_DataTable2", dt2));
        }


        private DataTable loadData1()
        {
            DataSet1.DataTable1DataTable dt = new DataSet1.DataTable1DataTable();

            DataRow dr1 = dt.NewRow();
            dr1["姓名"] = "王三";
            dr1["日期"] = DateTime.Now.ToShortDateString();
            dr1["金额"] = 130.45;
            dr1["余额"] = 1234.33;
            dt.Rows.Add(dr1);

            DataRow dr2 = dt.NewRow();
            dr2["姓名"] = "李四";
            dr2["日期"] = DateTime.Now.ToShortDateString();
            dr2["金额"] = 111.33;
            dr2["余额"] = 22.3;
            dt.Rows.Add(dr2);

            return dt;
        }


        private DataTable loadData2()
        {
            DataSet1.DataTable2DataTable dt2 = new DataSet1.DataTable2DataTable();
            DataRow d1 = dt2.NewRow();
            d1["姓名"] = "王三";
            d1["编号"] = "0001";
            d1["名称"] = "床位费";
            d1["价格"] = 10;
            d1["数量"] = 1;
            d1["金额"] = 10;
            dt2.Rows.Add(d1);

            DataRow d2 = dt2.NewRow();
            d2["姓名"] = "王三";
            d2["编号"] = "0002";
            d2["名称"] = "护理费";
            d2["价格"] = 8;
            d2["数量"] = 3;
            d2["金额"] = 24;
            dt2.Rows.Add(d2);

            DataRow d3 = dt2.NewRow();
            d3["姓名"] = "李四";
            d3["编号"] = "0002";
            d3["名称"] = "注射费";
            d3["价格"] = 8;
            d3["数量"] = 3;
            d3["金额"] = 24;
            dt2.Rows.Add(d3);

            return dt2;
       
        } 

其中LocalReport_SubreportProcessing这个方法需手工加,再给报表控件加上这个事件的引用,在这个事件中给子报表加数据库源,才可显示子报表。

 

另外,可以通过

string txt = first.ToShortDateString() + " 至 " + last.ToShortDateString();
            ReportParameter rp = new ReportParameter("Report_Parameter_0", txt);
            reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter[] { rp });

这种方法从外部给报表传递参数值,当然,需事先在报表中设计好相应的控件接收这个参数值。这主要用于显示如表头,表尾的一些信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在WinForms中使用RDLC报表的教程如下: 1. 添加RDLC报表控件:在Visual Studio中打开你的WinForms项目,找到“工具箱”窗口,在其中找到“Reporting”部分,右键点击并选择“选择项”。在弹出的对话框中,勾选“Microsoft.ReportViewer.WinForms”并点击确定。然后将“ReportViewer”控件拖放到你的窗体上。 2. 创建RDLC报表:在解决方案资源管理器中右键点击你的项目,选择“添加”->“新建项”,然后选择“报表”模板。在弹出的对话框中选择“报表向导”,按照向导的指引来创建你的RDLC报表。 3. 设计RDLC报表:在报表设计器中,你可以定义报表的布局、数据源和数据绑定。你可以使用表格、图表、文本框等控件来展示数据。在设计完报表后,保存并关闭报表设计器。 4. 加载数据到RDLC报表:在你的WinForms窗体中,你可以通过代码来加载数据到RDLC报表中。首先,创建一个数据源,可以是DataTable或者其他集合类型。然后,创建一个ReportDataSource对象,并将数据源赋值给它的Value属性。最后,将ReportDataSource对象添加到ReportViewer控件的LocalReport对象的DataSources集合中。 5. 显示RDLC报表:在代码中,使用ReportViewer控件的Refresh方法来刷新报表数据,并调用RefreshReport方法来显示报表。 这些是使用RDLC报表的基本步骤,你可以根据具体需求进一步定制和扩展报表功能。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值