RDLC报表系列--------钻取报表
钻取报表开发步骤:
先看总体效果:如图
点击单据编号
下面就做个看看...
1.先添加一个RDLC报表文件作为钻取报表.
添加一个RDLC报表,在设计界面添加一个表格Table,配置好数据源如图:
2.设置钻取报表对应的字段.
(1) 也许看到上面orderID为什么和别的不一样,多了个下划线了吧!不急,这里看怎么设置的.
鼠标点击orderID这列,右击文本框属性,找到字体选项卡如图:
在效果里将默认改为下划线。
(2)下面设置钻取报表.
然后再选择操作选项卡,第一步:设置启用为超链接为:转到报表(R).第二步:设置指定报表,指定报表里面填写钻取子报表的报表名如(DrillThoughReport) 不要加.rdlc 后缀名哦.第三步:添加参数,添加参数名和参数的值(值可以是Table 的列值和参数等)
总体设置如图:
点击确认.下一步设置子报表了.
3.设置子报表
转到子报表里,添加子报表的参数,这个参数来自哪里呢?当然是前面主报表里面设置过的参数啊,所以在这里添加参数,参数名orderID(注意参数名的大小写)并选中允许空白值和Null值.然后点击确认.最后在子报表里面添加数据就OK了.(主报表的数据源实际和子报表的数据源是一对多的关系 这里的主外键就是orderID啦)
如图:
好了。设置报表阶段基本完成.下面就代码了。
4.页面html 代码
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test.Default" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<rsweb:ReportViewer ID="ReportViewer1" Width="100%" runat="server"
ondrillthrough="ReportViewer1_Drillthrough">
<LocalReport ReportPath="Report.rdlc">
</LocalReport>
</rsweb:ReportViewer>
</div>
</form>
</body>
</html>
5.后台CS 文件代码
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Reporting.WebForms;
using Microsoft.Reporting;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Diagnostics;
namespace Test
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Display();
}
}
/// <summary>
/// 显示报表数据
/// </summary>
private void Display()
{
DataTable dt = GetReportData();
ReportViewer1.LocalReport.EnableHyperlinks = true;
ReportViewer1.Visible = true;
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));
ReportViewer1.LocalReport.Refresh();
}
/// <summary>
/// 钻取的主表
/// </summary>
/// <returns></returns>
public DataTable GetReportData()
{
#region 创建钻取的主表
DataTable dt = new DataTable("dt_dry");
dt.Columns.Add(new DataColumn("orderid", typeof(string)));
dt.Columns.Add(new DataColumn("userName", typeof(string)));
dt.Columns.Add(new DataColumn("address", typeof(string)));
dt.Columns.Add(new DataColumn("orderDate", typeof(DateTime)));
DataRow dr = dt.NewRow();
dr["orderid"] = "20120209001";
dr["userName"] = "张三";
dr["address"] = "北京";
dr["orderDate"] = DateTime.Now;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["orderid"] = "20120209002";
dr["userName"] = "李四";
dr["address"] = "上海";
dr["orderDate"] = DateTime.Now;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["orderid"] = "20120209003";
dr["userName"] = "王五";
dr["address"] = "北京";
dr["orderDate"] = DateTime.Now;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["orderid"] = "20120209004";
dr["userName"] = "马六";
dr["address"] = "湖南";
dr["orderDate"] = DateTime.Now;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["orderid"] = "20120209005";
dr["userName"] = "袁七";
dr["address"] = "湖北";
dr["orderDate"] = DateTime.Now;
dt.Rows.Add(dr);
return dt;
#endregion
}
/// <summary>
/// 子报表数据
/// </summary>
/// <returns></returns>
public DataTable GetTable()
{
DataTable dt = new DataTable("dt_dry");//创建一个名为dt_dry的DataTalbe
dt.Columns.Add(new DataColumn("fitemno", typeof(int)));//为dt_dry表内建立Column
dt.Columns.Add(new DataColumn("fitemName", typeof(string)));
dt.Columns.Add(new DataColumn("fsaleamt", typeof(decimal)));
dt.Columns.Add(new DataColumn("fqty", typeof(int)));
dt.Columns.Add(new DataColumn("orderid", typeof(string)));
DataRow dr = dt.NewRow();
dr[0] = 100;
dr[1] = "C#入门经典";
dr[2] = 87.67;
dr[3] = 2;
dr[4] = "20120209001";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 101;
dr[1] = "C#程序设计";
dr[2] = 67.58;
dr[3] = 5;
dr[4] = "20120209001";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 102;
dr[1] = "SQL 2005 从入门到精通";
dr[2] = 99.95;
dr[3] = 7;
dr[4] = "20120209002";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 103;
dr[1] = "asp.net 入门经典";
dr[2] = 88.93;
dr[3] = 9;
dr[4] = "20120209004";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 104;
dr[1] = "php 经典案例教材";
dr[2] = 45.55;
dr[3] = 13;
dr[4] = "20120209003";
dt.Rows.Add(dr);
return dt;
}
/// <summary>
/// 报表钻取事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ReportViewer1_Drillthrough(object sender, DrillthroughEventArgs e)
{
DataTable dt = GetTable();
LocalReport localReport = (LocalReport)e.Report;
localReport.DataSources.Add(new ReportDataSource("DataSet1", dt));
localReport.Refresh();
}
}
}
好咯.搞定了.