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. dr.wondr博士随笔之三星某智能机的SGHXXXX 的取证恢复一例

    大家好!欢迎今天再次来到我dr.wonde的博客, 今天我给大家带来一款三星镜面古董机SGH-E848的取证展示! 三星SGH-E848是一款非常漂亮的镜面手机,2008年出厂.. 上面黄色是97号数 ...

  2. Linux驱动程序学习【转】

    本文转载自: 一直在学习驱动,对于下面这篇文章,本人觉得简洁明了,基本符合我们学习驱动的进度与过程,现转发到自己的博客,希望能与更多的朋友分享. 了解Linux驱动程序技巧学习的方法很重要,学习lin ...

  3. loadrunner总体使用篇

    为什么要进行性能测试呢?  有些问题是只有在大并发或者压力测试下才会暴露出来的,在平常的公司内部测试中,感觉一切都是正常的,但是把服务放到生产线上,例如某个时刻突然有很多的用户要向我们的服务发送请求, ...

  4. Centos7下dnscrypt-proxy安装

    DNS劫持指的是"一些设备"会拦截DNS解析请求,并将错误的DNS信息返回给主机:DNS污染指的是"一些设备"会将错误的域名信息下发至公共的DNS服务器中(比如 ...

  5. java中的队列

    转载自:http://blog.csdn.net/guijava/article/details/3784658 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.Qu ...

  6. MD5 Checksums for R12.1.1 Rapid Install Media (文档 ID 802195.1)

    Oracle EBusiness Suite R12.1.x Rapid Installmd5 Checksums November 2011 Section      1: Overview Sec ...

  7. sqlserver2008一直显示正在还原

      sqlserver2008一直显示正在还原.如果不需要还原,则使用: restore database test with recovery如果只需要还原,则使用: restore databas ...

  8. win7和u盘redhat7.1双系统安装总结

    最近win7系统越用越卡,又没钱买mac只能想办法装以下linux系统,听说redhat服务器用的比较多,就想尝试一下装一个redhat.当然,和所有人一样,搜索了很多资料.我选择装双系统,因为要抛弃 ...

  9. R画图中英文字体完美解决方案

    在某些时候,需要在R画图中添加中文,但是默认情况下,R对中文的支持不好. 当用R画PDF图,并且图中有中文的时候,安装并加载如下包library(showtext)然后:showtext.auto(e ...

  10. drawable animation

    drawable 动画,帧动画: 1 定义动画xml文件 <?xml version="1.0" encoding="utf-8"?> <an ...