在要WinForms 应用(.NET 8.0)中,显示RDLC报表,就要使用ReportViewerCore.WinForms。原来的ReportViewer只能在.NET Framework框架下运行。
1.ReportViewerCore.WinForms 程序包
使用NuGet安装
SQL Server Reporting Services ReportViewer WinForms control decompiled and recompiled for .NET Core. Based on ReportViewer 15.0.1404.0
2.主要代码
ReportViewerForms.cs
using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ReportViewerCore
{
class ReportViewerForm : Form
{
private readonly ReportViewer reportViewer;
public ReportViewerForm()
{
Text = "ReportViewerCore.WinForms示例(目标框架.NET 8.0)";
//WindowState = FormWindowState.Maximized;
this.Width = 1000;
this.Height = 600;
reportViewer = new ReportViewer();
reportViewer.Dock = DockStyle.Fill;
Controls.Add(reportViewer);
// 设置打印布局模式,显示物理页面大小
this.reportViewer.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
// 缩放模式为百分比,以100%方式显示
this.reportViewer.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
this.reportViewer.ZoomPercent = 100;
}
protected override void OnLoad(EventArgs e)
{
Report.Load(reportViewer.LocalReport);
reportViewer.RefreshReport();
base.OnLoad(e);
}
private void ReportViewerForm_Load(object sender, EventArgs e)
{
}
private void InitializeComponent()
{
SuspendLayout();
//
// ReportViewerForm
//
ClientSize = new System.Drawing.Size(784, 472);
Name = "ReportViewerForm";
StartPosition = FormStartPosition.CenterScreen;
Load += ReportViewerForm_Load;
ResumeLayout(false);
}
}
}
using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ReportViewerCore
{
class Report
{
public static void Load(LocalReport report)
{
var items = new[] { new ReportItem { Description = "Widget 6000", Price = 104.99m, Qty = 1 }, new ReportItem { Description = "Gizmo MAX", Price = 1.41m, Qty = 25 } };
var parameters = new[] { new ReportParameter("Title", "Invoice 4/2020") };
using var fs = new FileStream("Report.rdlc", FileMode.Open);
report.LoadReportDefinition(fs);
report.DataSources.Add(new ReportDataSource("Items", items));
report.SetParameters(parameters);
}
}
}
3.实例窗口
4.使用DataSet加载数据
使用TableAdapter+DataSet读取报表数据。用DataSet1.xsd文件,配置TableAdapter和DataSet。
private void button2_Click(object sender, EventArgs e)
{
try
{
ShowReport("Report1");//显示报表Report1.rdlc
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ShowReport(string strReportName)
{
//使用TableAdapter+DataSet读取报表数据。用DataSet1.xsd文件配置好TableAdapter和DataSet。
try
{
this.reportViewer.LocalReport.ReportPath = ".\\" + strReportName + ".rdlc";
this.reportViewer.LocalReport.DataSources.Clear();
this.testTaskTableAdapter1.Fill(this.dataSet1.TestTask);
ReportDataSource rds1 = new ReportDataSource();
rds1.Name = "DataSet1";
rds1.Value = this.dataSet1.TestTask;
reportViewer.LocalReport.DataSources.Add(rds1);
reportViewer.RefreshReport();
}
catch (Exception ex)
{
MessageBox.Show("加载报表出错,请联系系统管理员。" + ex.Message);
}
}
警告 SYSLIB0051 “DataSet.DataSet(SerializationInfo, StreamingContext, bool)”已过时:
5.使用DataTable加载数据
因警告 SYSLIB0051 “DataSet.DataSet(SerializationInfo, StreamingContext, bool)”已过时,在.NET 5 和更高版本中已被标记为不再使用。从.NET 5开始,推荐使用DataTable等传统类型进行序列化。
改用SqlConnection、SqlDataAdapter和DataTable读取数据。
private void ShowReport1(string strReportName)
{
this.reportViewer.LocalReport.ReportPath = ".\\" + strReportName + ".rdlc";
this.reportViewer.LocalReport.DataSources.Clear();
DataTable dTable = new DataTable();
string sqlString = "SELECT IDD, 检测编号, 受检单位, 受检单位地址, 送样日期, 受理日期, 采样点数, 备注 FROM TestTask";
dTable = GetDataTable(sqlString);
if (dTable == null)
{
dTable = new DataTable();
}
ReportDataSource rds1 = new ReportDataSource();
rds1.Name = "DataSet1";
rds1.Value = dTable;
reportViewer.LocalReport.DataSources.Add(rds1);
reportViewer.RefreshReport();
}
private static DataTable GetDataTable(string strSQL, params IDataParameter[] sqlParams)
{
DataTable dt = new DataTable();
//数据库连接串
string strConn = "Data Source=XXXXXXXX\\MSSQL2019;Initial Catalog=LMIS;Integrated Security=True;Encrypt=False";
SqlConnection conn;
try
{
conn = new SqlConnection(strConn);
conn.Open();//打开数据库
string sql = strSQL;
SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //创建SqlDataAdapter类的对象
System.Data.DataSet ds = new System.Data.DataSet(); //创建DataSet
sda.Fill(ds);//使用SqlDataAdapter,将查询数据填充到DataSet
dt = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show("加载数据错误!" + ex.Message);
}
return dt;
}