在要WinForms 应用(.NET 8.0)中,显示RDLC报表,就要使用ReportViewerCore.WinForms。原来的ReportViewer只能在.NET Framework框架下运行。
1.ReportViewerCore.WinForms 程序包说明
SQL Server Reporting Services ReportViewer WinForms control decompiled and recompiled for .NET Core. Based on ReportViewer 15.0.1404.0
2.主要程序ReportViewerForms.cs
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);
}
}
}
cs
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.实例窗口