using Microsoft.Reporting.WinForms;
using System;
using System.Data;
using System.Windows.Forms;
namespace WindowsFormsApp13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ReportViewer reportViewer = new ReportViewer();//创建报表
private void button1_Click(object sender, EventArgs e)
{
reportViewer.ProcessingMode = ProcessingMode.Local;//设置本地处理模式
reportViewer.Dock = DockStyle.Fill;//设置报表控件填满窗体
this.groupBox1.Controls.Add(reportViewer);//填加报表控件到窗体
reportViewer.LocalReport.DataSources.Clear();//清空报表数据
reportViewer.LocalReport.ReportEmbeddedResource = "WindowsFormsApp13.Report1.rdlc";//设置rdlc文件,ReportEmbeddedResource(嵌入式模式)
//reportViewer1.LocalReport.ReportPath = "Book.Report1.rdlc";
reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", LoadDepartmentsData()));//DataSet1是设计报表时指定的数据集名称
reportViewer.Drillthrough += new DrillthroughEventHandler(DemoDrillthroughEventHandler);//添加钻取事件
reportViewer.LocalReport.Refresh();
reportViewer.RefreshReport();//刷新报表,这句一定要有
}
private DataTable LoadDepartmentsData()
{
DataSet dt = new DataSet();
string sql = "select Bookauthor, ";
sql += "sum(case when TypeName='操作系统' then bookprice else 0 end) as 操作系统, ";
sql += "sum(case when TypeName='办公软件' then bookprice else 0 end) as 办公软件, ";
sql += "sum(case when TypeName='计算机语言' then bookprice else 0 end) as 计算机语言 ";
sql += "from (select BookAuthor,TypeName,BookPrice from book left join BookType on book.BookType=BookType.TypeId) as a group by BookAuthor";
dt = SqlHelper.SqlHelper.GetDataSet(sql);
return dt.Tables[0];
}
private DataTable LoadEmployeesData()//获取要钻取的数据表
{
DataSet dt = new DataSet();
string sql = "select BookAuthor,BookName,StorageInNum from book";
dt = SqlHelper.SqlHelper.GetDataSet(sql);
return dt.Tables[0];
}
void DemoDrillthroughEventHandler(object sender,DrillthroughEventArgs e)//给钻取报表添加数据源
{
LocalReport localReport = (LocalReport)e.Report;
localReport.DataSources.Add(new ReportDataSource("DataSet1", LoadEmployeesData()));
}
}
}