本篇博客将介绍如何在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中使用ReportViewer报表的更多相关文章

  1. 在 ReportViewer 报表中使用表达式

    from:http://www.cnblogs.com/jobin/articles/1152213.html 有些表达式在报表中很常用.其中包括更改报表中的数据外观的表达式.计算总数的表达式和更改报 ...

  2. [转]逐步解說:在 WPF 應用程式中使用 ReportViewer 显示 rdlc

    本文转自:http://msdn.microsoft.com/zh-tw/library/hh273267 若要在 WPF 應用程式中使用 ReportViewer 控制項,您需要將 ReportVi ...

  3. 在VS2012后的版本中做数据报表时,提示尚未指定报表“Report1”的报表定义

    有一群的朋友在用VS2012做数据报表时,老是提示 本地报表处理期间出错. 尚未指定报表“Report1”的报表定义 未将对象引用设置到对象的实例. 我看了一下,步骤没错,我用VS2010做了一下,一 ...

  4. ASP.NETserver控件使用之Reportviewer 报表

    1.       Reportviewer 报表 1.1.       Reportviewer控件 注:本教程附2个事例: l  演练:在本地处理模式下将数据库数据源与 ReportViewer W ...

  5. VSTO 学习笔记(六)在 Excel 2010中使用RDLC报表

    原文:VSTO 学习笔记(六)在 Excel 2010中使用RDLC报表 Excel具有强大的图表显示.分析功能,这点毋庸置疑,但是如果将常规MIS系统中的数据以报表的形式在Excel中显示,却并不那 ...

  6. 在Bootstrap开发框架中使用Grid++报表

    之前在随笔<在Winform开发中使用Grid++报表>介绍了在Winform环境中使用Grid++报表控件,本篇随笔介绍在Bootstrap开发框架中使用Grid++报表,也就是Web环 ...

  7. 在mvc视图中实现rdlc报表展示(补充)

    上篇: 在mvc视图中实现rdlc报表展示 在上一遍中,由于不知道如何在aspx.cs后台中实例化abp的IxxxAppService.cs的接口,我采取的方式是在视图页中把查询出的数据存储到aspx ...

  8. 在mvc视图中实现rdlc报表展示

    需求:在view视图页面中嵌入rdlc报表,rdlc的xml为动态传入的xml字符串.本项目是基于abp框架 可能出现问题: 1.rdlc报表是由asp.net的服务器控件ReportViewer来支 ...

  9. 在WPF中使用依赖注入的方式创建视图

    在WPF中使用依赖注入的方式创建视图 0x00 问题的产生 互联网时代桌面开发真是越来越少了,很多应用都转到了浏览器端和移动智能终端,相应的软件开发上的新技术应用到桌面开发的文章也很少.我之前主要做W ...

随机推荐

  1. ubuntu14.04 server 安装vmware worktation 12

    0) Do the basic system installation of Ubuntu 14.04 LTS (Server or Desktop) 1) wget the installer wg ...

  2. openstack 常用命令

    转自: docs.openstack.org $ nova boot --image ubuntu-cloudimage --flavor 1 --user-data mydata.file

  3. JavaScript——Prototype详探

    用过JavaScript的同学们肯定都对prototype如雷贯耳,但是这究竟是个什么东西却让初学者莫衷一是,只知道函数都会有一个prototype属性,可以为其添加函数供实例访问,其它的就不清楚了, ...

  4. ASP.NET MVC 如何解决“上下文的模型已在数据库创建后发生更改”问题

    问题描述:支持"XXContext"(泛指之类的数据库上下文模型)上下文的模型已在数据库创建后发生更改.请考虑使用 Code First 迁移更新数据库. 问题解决:坑爹的MVC会 ...

  5. 《Head First Servlet JSP》web服务器,容器,servlet的职责

    (一)web服务器,容器,servlet的职责 (二)J2EE服务器与web容器

  6. meeting room I & II

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  7. python之打包相关

    打包手册:https://python-packaging-user-guide.readthedocs.org/en/latest/installing.html#installing-from-a ...

  8. Linux-PAM认证模块

    Linux-PAM认证模块     用户访问服务器的时候,服务器的某一个服务程序把用户的谁请求发送到PAM模块进行认证.对于不同的服务器应用程序所对应的PAM模块也是不同的.如果想查看某个程序是否支持 ...

  9. PHP生命周期

    2015-08-19 15:05:30 周三 一篇很好的文章 PHP内核探索 总结一下 1. 模块初始化 MINIT 各个PHP模块/扩展初始化内部变量, 告诉PHP调用自己的函数时, 函数体在哪里( ...

  10. ffmpeg-20160527-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...