1、新建wpf项目,并引入3个程序集:

Microsoft.ReportViewer.WinForms

WindowsFormsIntegration

System.Windows.Forms

如果无法搜索到,可能是VS没有安装相关组件,请如图添加:

2、新建Entities文件夹,在其中添加Entities.cs文件,其中创建两个实体类,Department和Employee

class Department
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
public string DepartmentInfo { get; set; }
} class Employee
{
public int EmployeeId { get; set; }
public int DepartmentId { get; set; }
public string Name { get; set; }
public string NickName { get; set; }
}

3、创建钻取表——部门表Department.rdlc,内部控件可在工具箱窗口创建:

  3.1、绑定数据集/源,在Report Data中的数据集项目右键-添加数据集(名称为:DepartmentDS)-新建数据源-对象,下一步选择sep2中建立的Department对象。

  3.2、完成后即可将数据源的字段添加如报表中。

  3.3、在DepartmentId中右键-文本框属性-操作-选择“转到报表”,指定报表填入“Emploee”(此报表下一步创建)参数填入DepartmentId:

  

  再设置一下钻取关键字的字体样式,钻取表配置完成。

4、创建目标表——员工表Employee:

  4.1、添加绑定Employee对象,操作如部门表,数据集名称指定为:(名称为:EmployeeDS)。

  4.2、定义参数,Report Data窗口中:参数右键-添加参数,名称填入DepartmentId,类型为Integer(要与传入类型一致)。

  4.3、添加筛选器,筛选器可以采用指定参数来自动过滤数据,选择数据集所在行右键-tablix属性-筛选器-点击fx进入表达式编写。

  其中表达式填入:=CInt(Fields!DepartmentId.Value)类型为Integer,运算符为“=”,值填入:=CInt(Parameters!DepartmentId.Value)

  到此,报表主要设计完成。

5、在MainWindow窗体中放入ReportViewer:

  5.1、xaml窗口代码,先引入命名空间:xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"

  然后主体部分:

  

    <Grid>
<WindowsFormsHost>
<rv:ReportViewer x:Name="reportViewer"/>
</WindowsFormsHost>
</Grid>

  5.2、窗口交互代码,功能是为报表填充数据,数据介质是DataTable,内容正是对应的实体类。

  

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += DepartmentLoaded;
//this.Loaded += EmployeeLoaded;
} // 载入部门报表
private void DepartmentLoaded(object sender, RoutedEventArgs e)
{
// 模拟一个DataTable
DataTable dt = new DataTable();
dt.Columns.Add("DepartmentId", typeof(int));
dt.Columns.Add("DepartmentName", typeof(string));
dt.Columns.Add("DepartmentInfo", typeof(string)); dt.Rows.Add(, "内勤", "B3456123");
dt.Rows.Add(, "外勤", "U3884912"); ReportDataSource reportDataSource = new ReportDataSource(); reportDataSource.Name = "DepartmentDS";
reportDataSource.Value = dt; reportViewer.Reset(); reportViewer.LocalReport.ReportPath = Directory.GetCurrentDirectory() + "\\Department.rdlc";
reportViewer.LocalReport.DataSources.Add(reportDataSource);
reportViewer.Drillthrough += new DrillthroughEventHandler(report_Drillthrough);// 处理钻取事件 reportViewer.RefreshReport();
} // 载入员工报表
private void EmployeeLoaded(object sender, RoutedEventArgs e)
{
// 模拟一个DataTable
DataTable dt = MakeEmployeeDT(); ReportDataSource reportDataSource = new ReportDataSource(); reportDataSource.Name = "EmployeeDS";
reportDataSource.Value = dt; reportViewer.Reset(); reportViewer.LocalReport.ReportPath = Directory.GetCurrentDirectory() + "\\Employee.rdlc";
reportViewer.LocalReport.DataSources.Add(reportDataSource); ReportParameter departmentId = new ReportParameter("DepartmentId", "");
reportViewer.LocalReport.SetParameters((new ReportParameter[] { departmentId }));// 放入参数测试 reportViewer.RefreshReport();
} // 填充钻取的数据
private void report_Drillthrough(object sender, DrillthroughEventArgs e)
{
int dId = ;
DataTable dt = MakeEmployeeDT(); ReportDataSource reportDataSource = new ReportDataSource(); reportDataSource.Name = "EmployeeDS";
reportDataSource.Value = dt; Report newRV = e.Report; if (newRV.GetParameters().Count > && newRV.GetParameters()["DepartmentId"] != null)
{
string tmp = newRV.GetParameters()["DepartmentId"].Values[].Trim();
dId = Int32.Parse(tmp, );
}
LocalReport localReport = (LocalReport)e.Report;
localReport.DataSources.Add(reportDataSource); //reportViewer.RefreshReport();// 钻取操作不能refresh,否则会导致刷新窗口,清除钻取记录
} // 创建员工数据表
private DataTable MakeEmployeeDT()
{
DataTable dt = new DataTable();
dt.Columns.Add("EmployeeId", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("NickName", typeof(string));
dt.Columns.Add("DepartmentId", typeof(string)); dt.Rows.Add(, "张三", "John", );
dt.Rows.Add(, "李四", "Joo", );
return dt;
} }

总结/说明:

  1、本例使用实体对象作为绑定数据源,可以较好的整合进当前项目,维持现有架构的层级关系,另外也可直接配置数据库或WCF服务,请自行测试;

  2、新建数据源时若找不到step2建立的实体类,请重新编译项目

  3、初次接触者RDLC文件的xml源码图形化的布局设计都定义在里面。

  4、钻取目标表的结构结果筛选即可由rdlc定义完成,也可以获取参数后自行组装DataTable,前者简单省事,后者性能高一些,看需求~。

最后,贴一张运行效果图:

程序打包下载

WPF中RDLC报表的钻取实现的更多相关文章

  1. Vs2010中rdlc报表绑定DataTable数据源

    首先,新建一个网站,接着添加数据集,并且命名为student,如下图所示: 在该数据集对象上面添加datatable,并且设置列名,如下图所示: 添加一张报表,命名为student,如下图所示: 向报 ...

  2. C#中RDLC报表常用表达式(字符串和转换)

    字符串函数 (1)使用串联运算符和 Visual Basic 常量可将多个字段组合在一起.以下表达式返回两个字段,它们分别位于同一文本框的不同行中:=Fields!FirstName.Value &a ...

  3. C#中RDLC报表判断某字段的值为null

    =iif(Isnothing(Fields!VerifyStateName.Value),"未上报",Fields!VerifyStateName.Value)   空值时赋予默认 ...

  4. C#中RDLC报表中日期显示格式

    转换为日期类型再格式化 =CDate(Fields!UseDate.Value).ToString("yyyy-MM-dd") 使用Format ==Format(Fields!C ...

  5. win10下rdlc报表在vs(visual studio)中中文显示小方块的批量处理解决方法

    在网上找vs中rdlc报表显示中文时显示小方块的解决方案,无外就是修改文本框的字体属性.但是对于维护已有的rdlc报表时,有中文的地方(此时都显示了小方块)会很多,再一个一个设置实在太麻烦.所以自己花 ...

  6. [转]使用RDLC报表

    使用RDLC报表(一) 1       建立数据源 启动VS2005新建一个窗体项目,命名为TestProj 在左边的窗体内选择“添加新数据源”或在菜单上操作“添加新数据源”: 选择后出现对话窗体,选 ...

  7. WPF中使用ReportViewer报表

    本篇博客将介绍如何在WPF中使用ReportViewer控件. 1. 环境准备:下载安装最新版ReportViewer(PS:需要安装Microsoft SQL Server System CLR T ...

  8. 让rdlc报表在ReportViewer中水平居中的方法

    正常情况下,rdlc报表在Reportviewer中是居左显示的,如图: 在Reporviewer的属性中,我没有找到能让rdlc的居中显示的方法.网上其他人用的方法都试了,没能实现,只能自己找方法解 ...

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

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

随机推荐

  1. win下命令行替代品Cmder

    Cmder简单使用小结 Cmder是一款Windows环境下非常简洁美观易用的cmd替代者,它支持了大部分的Linux命令. 从官网下载下来一个zip安装包,解压之后运行根目录的Cmder.exe即可 ...

  2. C# ListView得到选中项及子项

    private void listViewEx_MouseClick(object sender, MouseEventArgs e) { ListViewItem lv = listViewEx.G ...

  3. Linux命令的返回码列表

    转自:http://blog.chinaunix.net/uid-10347480-id-3263127.html 在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令,所有的程序 ...

  4. 接口测试之HTTP协议详解

    引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1. ...

  5. iOS开发零碎知识点

    记录一些常用和不常用的iOS知识点,防止遗忘丢失.(来源为收集自己项目中用到的或者整理看到博客中的知识点),如有错误,欢迎大家批评指正:如有好的知识点,也欢迎大家联系我,添加上去.谢谢! 一.调用代码 ...

  6. asp.net mvc adminlte第一波

    首页模板选用官方DEMO中的Blank模板,这个模板相对来说是最干净的. 首页模板的分割: 官方文档是分的4个部分 Wrapper .wrapper. A div that wraps the who ...

  7. args[0]

    java程序有一个主方法,是这样的public static void main(String [] args)你说的args[0]就是你用命令行编译运行java程序时,传入的第一个参数,比如你运行一 ...

  8. c#DataGridView数据绑定示例——格式化单元格的内容(转)

    转自http://blog.csdn.net/testcs_dn/article/details/37834063 c#DataGridView数据绑定示例 格式化单元格的内容 在使用DataGrid ...

  9. GreenDao介绍

    GreenDao介绍 greenDAO 是一个将对象映射到 SQLite 数据库中的轻量且快速的 ORM 解决方案 何为ORM? ORM(Object/Relation Mapping): 对象/关系 ...

  10. 非本地跳转之setjmp与longjmp

    非本地跳转(unlocal jump)是与本地跳转相对应的一个概念. 本地跳转主要指的是类似于goto语句的一系列应用,当设置了标志之后,可以跳到所在函数内部的标号上.然而,本地跳转不能将控制权转移到 ...