zoukankan      html  css  js  c++  java
  • WPF中使用ReportViewer报表

    本篇博客将介绍如何在WPF中使用ReportViewer控件。

    1. 环境准备:下载安装最新版ReportViewer(PS:需要安装Microsoft SQL Server System CLR Types package);如果您的开发工具是Visual Studio 2015,记得安装Microsoft SQL Server Tools,因为需要安装ReportViewer报表设计器。

    2. 下面我们通过一个例子(示例图书品种报表)来演示,

    1). 新建一个WPF项目WPFBooksReport,

    2). 添加Entities文件夹,并添加Book类,

    复制代码
        public class Book
        {
            public string Name { get; set; }
    
            public string Author { get; set; }
    
            public string ISBN { get; set; }
    
            public decimal Price { get; set; }
        }
    复制代码

    3). 添加名称为BookReport的RDLC报表,

    报表设计器主界面

    修改报表属性:

    4. 新建DataSet,名称BookDataSet,然后新建DataSource,DataSource的数据来源于Object,因为在示例程序中为了降低复杂度,直接使用Book类作为数据来源了。

    这样,RDLC报表的数据源便设置成功了。下一步设计报表的样子。

    5). 在报表中插入一个Table,然后设置数据源,

    6). 新建WPF UserControl,BookReportCtrl.xaml,在项目中添加Microsoft.ReportViewer.WinForms和WindowsFormsIntegration引用

    BookReportCtrl.xaml

    复制代码
    <UserControl x:Class="WPFBooksReport.BookReportCtrl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
                 xmlns:local="clr-namespace:WPFBooksReport"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <WindowsFormsHost>
                <rv:ReportViewer x:Name="bookReportViewer"/>
            </WindowsFormsHost>
            <local:MaskLayer x:Name="maskLayer" Visibility="Collapsed"/>
        </Grid>
    </UserControl>
    复制代码

    Code:

    复制代码
            public BookReportCtrl()
            {
                InitializeComponent();
    
                this.Loaded += BookReportCtrl_Loaded;
    
                this.bookReportViewer.RenderingComplete += BookReportViewer_RenderingComplete;
            }
    
            private void BookReportCtrl_Loaded(object sender, RoutedEventArgs e)
            {
                maskLayer.Visibility = Visibility.Visible;
    
                // 模拟一个DataTable
    
                DataTable dt = new DataTable();
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Author", typeof(string));
                dt.Columns.Add("Price", typeof(decimal));
                dt.Columns.Add("ISBN", typeof(string));
    
                DataRow dr = dt.NewRow();
                dr["Name"] = "C# In Depth";
                dr["Author"] = "Jon Skeet";
                dr["Price"] = 72.0m;
                dr["ISBN"] = "B3456123";
    
                dt.Rows.Add(dr);
    
                ReportDataSource reportDataSource = new ReportDataSource();
                
                reportDataSource.Name = "BookDataSet";
                reportDataSource.Value = dt;
    
                bookReportViewer.LocalReport.ReportPath = Directory.GetCurrentDirectory() + "\BookReport.rdlc";
                bookReportViewer.LocalReport.DataSources.Add(reportDataSource);
    
                bookReportViewer.RefreshReport();
            }
    
            private void BookReportViewer_RenderingComplete(object sender, Microsoft.Reporting.WinForms.RenderingCompleteEventArgs e)
            {
                maskLayer.Visibility = Visibility.Collapsed;
            }
    复制代码

    6. 新建BookReportWindow.xaml来承载报表。

    7. 运行程序,

    到这里,这个示例程序就完成了。

    代码点击这里下载。

    感谢您的阅读。

  • 相关阅读:
    python常用字符串处理(转)
    Python读写文件基础.py
    Python异常和调试.md
    Python类的进阶.md
    Python模块和类.md
    python函数基础:调用内置函数&定义函数
    Spring.Net学习笔记(二)-数据访问器
    Spring.Net学习笔记(1)-容器的使用
    Spring.Net学习笔记(0)-错误汇总
    VS中设置xml智能提示
  • 原文地址:https://www.cnblogs.com/wangchaoyuana/p/7523632.html
Copyright © 2011-2022 走看看