博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF中使用ReportViewer报表
阅读量:7235 次
发布时间:2019-06-29

本文共 2382 字,大约阅读时间需要 7 分钟。

原文:

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

1. 环境准备:下载安装最新版(PS:需要安装);如果您的开发工具是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

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. 运行程序,

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

代码点击下载。

感谢您的阅读。

转载地址:http://nqgfm.baihongyu.com/

你可能感兴趣的文章
(十七)java冒泡排序和compareto
查看>>
linux内存查看方式
查看>>
Java魔法堂:String.format详解-
查看>>
线性重复动画
查看>>
炒冷饭系列:设计模式 建造者模式
查看>>
BAT解密:互联网技术发展之路(2)- 业务如何驱动技术发展
查看>>
bat脚本自动扫描制定文件夹下shp文件,并导入数据库,然后执行空间操作
查看>>
关于AsyncHttpClient的cz.msebera.android.httpclient.Header
查看>>
Codekit - 为Web前端打造的全能型神器(附推荐各种工具)
查看>>
JSP JSTL SQL标签操作数据库
查看>>
compare python use py-postgresql & direct pgbench's performance
查看>>
【hibernate框架】核心开发接口-update方法
查看>>
Android过场动画基础教程
查看>>
实现指定任意数量的方块EditText容器BlockEditTextViewGroup
查看>>
PostCSS 常用插件与语法介绍
查看>>
计算机是如何存储数据的?
查看>>
Decorator:从原理到实践,我一点都不虚~
查看>>
前端引用字体@font-face的若干优化方法
查看>>
Linux笔记---修改文件所有者、所属组
查看>>
为什么开发kedis-server
查看>>