一个通用的报表显示窗口(WindowsForms)

这也是这两天课堂上的一个范例。我们考虑到有很多报表,不可能为所有的报表单独定义一个窗口来显示,所以最后重构成一个通用的窗口

image

后台代码

using System;
using Microsoft.Reporting.WinForms;

namespace NorthwindApplication
{
    public partial class ReportForm : NorthwindApplication.FormTemplate
    {
        public ReportForm()
        {
            InitializeComponent();
        }

        public ReportForm(
            string formTitle,
            string reportTitle,
            string reportPath,
            params ReportDataSource[] datasource)
            : this()
        {
            this.Text = formTitle;
            this.FormTitle.Text = reportTitle;
            this.reportViewer1.LocalReport.ReportPath = reportPath;
            this.reportViewer1.LocalReport.DataSources.Clear();
            foreach (var item in datasource)
            {
                this.reportViewer1.LocalReport.DataSources.Add(item);
            }

        }

        private void ReportForm_Load(object sender, EventArgs e)
        {

            this.reportViewer1.RefreshReport();
        }
    }
}

主窗体调用代码

           //这里读取数据,然后实例化报表,然后显示报表
            var formTitle = "员工订单报表";
            var context = new NorthwindDataContext();

            var employee =
                context.Employees.Where(
                    emp => emp.EmployeeID == 1);

            

            if (employee != null)
            {
                var temp = employee.FirstOrDefault();
                var reportTitle = temp.FirstName + "," + temp.LastName;
                var reportPath = "EmployeeOrderReport.rdlc";

                ReportForm report = new ReportForm(
                    formTitle,
                    reportTitle,
                    reportPath,
                    new ReportDataSource(
                        "NorthwindApplication_Employees",
                        employee),
                    new ReportDataSource(
                        "NorthwindApplication_Orders",
                        temp.Orders.ToArray()));


                report.MdiParent = this;
                report.WindowState = FormWindowState.Maximized;
                report.Show();

            }

窗体显示的大致效果如下

 
image 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值