WPF中使用ReportViewer报表

简介: 原文:WPF中使用ReportViewer报表本篇博客将介绍如何在WPF中使用ReportViewer控件。 1. 环境准备:下载安装最新版ReportViewer(PS:需要安装Microsoft SQL Server System CLR Types package);如果您的开发工具是Visual Studio 2015,记得安装Microsoft SQL Server Tools,因为需要安装ReportViewer报表设计器。
原文: 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. 运行程序,

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

代码点击这里下载。

感谢您的阅读。

目录
相关文章
WPF使用DataGridComboBoxColumn完成绑定
 在使用DataGrid的时候,有时候需要使某些列为ComboBox,这时自然想到使用DataGridComboBoxColumn,但是如果使用的是ItemsSource数据绑定后台的对象,就会发现,这根本就不能用。
2436 0
|
C# 开发者 Windows
震撼发布:全面解析WPF中的打印功能——从基础设置到高级定制,带你一步步实现直接打印文档的完整流程,让你的WPF应用程序瞬间升级,掌握这一技能,轻松应对各种打印需求,彻底告别打印难题!
【8月更文挑战第31天】打印功能在许多WPF应用中不可或缺,尤其在需要生成纸质文档时。WPF提供了强大的打印支持,通过`PrintDialog`等类简化了打印集成。本文将详细介绍如何在WPF应用中实现直接打印文档的功能,并通过具体示例代码展示其实现过程。
1272 0
|
前端开发 C# Windows
在WPF程序中实现PropertyGrid功能
【11月更文挑战第15天】PropertyGrid 是一个用户界面组件,用于直观地查看和编辑对象属性。在 WPF 中可通过组合 Expander 和 DataGrid 实现基本功能,或使用第三方库 PropertyTools 获得更强大特性,包括属性验证和类型特定编辑器。
814 3
|
搜索推荐 API C#
.NET开源快速、强大、免费的电子表格组件
.NET开源快速、强大、免费的电子表格组件
247 0
|
JSON 小程序 数据格式
微信小程序动态修改页面标题title
微信小程序动态修改页面标题title
2165 0
微信小程序动态修改页面标题title
|
XML C# 数据格式
WPF技术之DocumentViewer控件
WPF 的 DocumentViewer 是一个强大的控件,用于在应用程序中显示各种类型的文档,如 XPS(XML Paper Specification)、FlowDocument 和 FixedDocument 等。
2562 1
|
Java C# 图形学
Unity——对象池
Unity——对象池
314 0
|
数据可视化 C# 开发工具
C#或Winform中的消息通知之系统本地通知(local toast notification)
C#应用通过 Microsoft.Toolkit.Uwp.Notifications NuGet包可以很方便的发送本地通知,适用于所有类型的应用(WPF、UWP、WinForms、控制台)
2378 0
C#或Winform中的消息通知之系统本地通知(local toast notification)
|
缓存 数据可视化 C#
WPF技术之Frame控件
WPF (Windows Presentation Foundation) Frame是WPF中的一个控件,它在应用程序中提供了一个容器,用于加载和显示其他页面或用户界面元素。
2046 0