using Microsoft.Reporting.WinForms;
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApp16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
shorbb();
timer1.Enabled = true;//启动计数器
}
int t = 0;//计数(秒)
ReportViewer reportViewer = new ReportViewer();//创建报表1
ReportViewer reportViewer1 = new ReportViewer();//创建报表2
private void shorbb()
{
//报表1
reportViewer.ProcessingMode = ProcessingMode.Local;//设置本地处理模式
reportViewer.Dock = DockStyle.Fill;//设置报表控件填满窗体
this.panel1.Controls.Add(reportViewer);//填加报表控件到窗体
reportViewer.LocalReport.DataSources.Clear();//清空报表数据
reportViewer.LocalReport.ReportEmbeddedResource = "WindowsFormsApp16.Report1.rdlc";//设置rdlc文件,ReportEmbeddedResource(嵌入式模式)
reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", LoadDepartmentsData()));//DataSet1是设计报表时指定的数据集名称
reportViewer.Drillthrough += new DrillthroughEventHandler(DemoDrillthroughEventHandler);//添加钻取事件
reportViewer.LocalReport.Refresh();
reportViewer.RefreshReport();//刷新报表,这句一定要有
//报表2
reportViewer1.ProcessingMode = ProcessingMode.Local;//设置本地处理模式
reportViewer1.Dock = DockStyle.Fill;//设置报表控件填满窗体
this.panel2.Controls.Add(reportViewer1);//填加报表控件到窗体
reportViewer1.LocalReport.DataSources.Clear();//清空报表数据
reportViewer1.LocalReport.ReportEmbeddedResource = "WindowsFormsApp16.Report2.rdlc";//设置rdlc文件,ReportEmbeddedResource(嵌入式模式)
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", LoadDepartmentsData()));//DataSet1是设计报表时指定的数据集名称
reportViewer1.Drillthrough += new DrillthroughEventHandler(DemoDrillthroughEventHandler);//添加钻取事件
reportViewer1.LocalReport.Refresh();
reportViewer1.RefreshReport();//刷新报表,这句一定要有
}
private DataTable LoadDepartmentsData()
{
string sql = "select BookName,BookAuthor from Book";
SqlDataReader reader= SqlHelper.SqlHelper.GetReader(sql);
if (!reader.HasRows)
{
return null;
}
else
{
DataTable dt = new DataTable();
dt.Load(reader);
return dt;
}
}
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()));
}
private void timer1_Tick(object sender, EventArgs e)//间隔10秒刷新一次
{
t++;
if (t==10)
{
shorbb();
t = 0;
}
}
}
}
原文:https://www.cnblogs.com/zhujie-com/p/12343822.html