zoukankan      html  css  js  c++  java
  • 一个通用的报表显示窗口(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 
  • 相关阅读:
    关于课内外读物的建议
    c# Aes加解密
    web api 如何通过接收文件流的方式,接收客户端及前端上传的文件
    c# 文件夹权限
    mysql 8创建远程访问用户以及连接mysql速度慢的解决方法
    为什么读书?读书让我们明心见性!
    大部分教程不会告诉你的 12 个 JS 技巧
    nuget包管理nuget服务器发布包时出现请求报错 406 (Not Acceptable)
    Python 实现毫秒级淘宝、京东、天猫等秒杀抢购脚本
    eos的资源和工具列表
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1699401.html
Copyright © 2011-2022 走看看