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中使用ReportViewer报表的更多相关文章
- 在 ReportViewer 报表中使用表达式
from:http://www.cnblogs.com/jobin/articles/1152213.html 有些表达式在报表中很常用.其中包括更改报表中的数据外观的表达式.计算总数的表达式和更改报 ...
- [转]逐步解說:在 WPF 應用程式中使用 ReportViewer 显示 rdlc
本文转自:http://msdn.microsoft.com/zh-tw/library/hh273267 若要在 WPF 應用程式中使用 ReportViewer 控制項,您需要將 ReportVi ...
- 在VS2012后的版本中做数据报表时,提示尚未指定报表“Report1”的报表定义
有一群的朋友在用VS2012做数据报表时,老是提示 本地报表处理期间出错. 尚未指定报表“Report1”的报表定义 未将对象引用设置到对象的实例. 我看了一下,步骤没错,我用VS2010做了一下,一 ...
- ASP.NETserver控件使用之Reportviewer 报表
1. Reportviewer 报表 1.1. Reportviewer控件 注:本教程附2个事例: l 演练:在本地处理模式下将数据库数据源与 ReportViewer W ...
- VSTO 学习笔记(六)在 Excel 2010中使用RDLC报表
原文:VSTO 学习笔记(六)在 Excel 2010中使用RDLC报表 Excel具有强大的图表显示.分析功能,这点毋庸置疑,但是如果将常规MIS系统中的数据以报表的形式在Excel中显示,却并不那 ...
- 在Bootstrap开发框架中使用Grid++报表
之前在随笔<在Winform开发中使用Grid++报表>介绍了在Winform环境中使用Grid++报表控件,本篇随笔介绍在Bootstrap开发框架中使用Grid++报表,也就是Web环 ...
- 在mvc视图中实现rdlc报表展示(补充)
上篇: 在mvc视图中实现rdlc报表展示 在上一遍中,由于不知道如何在aspx.cs后台中实例化abp的IxxxAppService.cs的接口,我采取的方式是在视图页中把查询出的数据存储到aspx ...
- 在mvc视图中实现rdlc报表展示
需求:在view视图页面中嵌入rdlc报表,rdlc的xml为动态传入的xml字符串.本项目是基于abp框架 可能出现问题: 1.rdlc报表是由asp.net的服务器控件ReportViewer来支 ...
- 在WPF中使用依赖注入的方式创建视图
在WPF中使用依赖注入的方式创建视图 0x00 问题的产生 互联网时代桌面开发真是越来越少了,很多应用都转到了浏览器端和移动智能终端,相应的软件开发上的新技术应用到桌面开发的文章也很少.我之前主要做W ...
随机推荐
- mysql 日志文件mysql-bin文件清除方法,和mysql-bin相关文件的配置
默认情况下mysql会一直保留mysql-bin文件,这样到一定时候,磁盘可能会被撑满,这时候是否可以删除这些文件呢,是否可以安全删除,是个问题. 首先要说明一下,这些文件都是mysql的日志文件,如 ...
- java web 学习 --第六天(Java三级考试)
第五天学习在这:http://www.cnblogs.com/tobecrazy/p/3458592.html session对象 当某个用户首次访问web应用系统时,jsp会自动创建出一个sessi ...
- ffmpeg-20160515-git-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- Effective C++ -----条款42:了解typename的双重意义
声明template参数时,前缀关键字class和typename可互换. 请使用关键字typename标识嵌套从属类型名称:但不得在base class lists(基类列)或member init ...
- JqueryUI学习笔记-自动完成autocomplete
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 51nod 1117 聪明的木匠 (哈夫曼树)
题目:传送门. 题意:中文题. 题解:就是构造一颗哈夫曼树,数据结构里的知识. #include <iostream> #include <cstdio> #include & ...
- Linux 命令执行结果输出到屏幕的同时写入到文件中
tee命令可以做到这一点: 例:ls -al /home | tee log 就可以把命令输出的内容显示在屏幕上的同时也输出至文件log.
- IOS - 多态
1. 多态性 多态性是个生物名词,用来表示生物体在生命周期中的不同形态,用在编程语言中则表示相同的方法名,但是却有不同的实现方式.或者说相同的名字,不同的类.我们来看一个书上的示例: #import ...