我想知道是否可以DataGridView
直接向rdlc报告发送/打印数据而不将其绑定到ReportViewer
控件.
关于将dgv数据绑定到报表查看器控件有很多线程.我不想使用报表查看器控件创建另一个表单,但是使用现有表单DataGridView
和打印按钮上的数据来发送数据以进行RDLC
报告和打印.
可能吗?
谢谢
打印不显示"打印"对话框
作为选项,您可以使用LocalReport
对象和CreateStreamCallback
回调函数以编程方式打印RDLC报告.这是一个完整的msdn演练:
演练:无需预览即可打印本地报告
打印显示"打印"对话框
作为另一个选项,将一个ReportViewer
on表格并将Visible
控件的属性设置为false,然后将数据传递给报表,当RenderingComplete
事件触发时,调用PrintDialog
:
ReportViewer.PrintDialog方法
对于第一个选项(没有显示打印对话框的打印),我创建了一个Print
扩展方法,您可以通过这种方式轻松使用它:
this.reportViewer1.LocalReport.Print();
这是扩展方法:
using Microsoft.Reporting.WinForms; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Printing; using System.IO; public static class LocalReportExtensions { public static void Print(this LocalReport report) { var pageSettings = new PageSettings(); pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize; pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape; pageSettings.Margins = report.GetDefaultPageSettings().Margins; Print(report, pageSettings); } public static void Print(this LocalReport report, PageSettings pageSettings) { string deviceInfo = $@""; Warning[] warnings; var streams = new List EMF {pageSettings.PaperSize.Width * 100}in {pageSettings.PaperSize.Height * 100}in {pageSettings.Margins.Top * 100}in {pageSettings.Margins.Left * 100}in {pageSettings.Margins.Right * 100}in {pageSettings.Margins.Bottom * 100}in (); var currentPageIndex = 0; report.Render("Image", deviceInfo, (name, fileNameExtension, encoding, mimeType, willSeek) => { var stream = new MemoryStream(); streams.Add(stream); return stream; }, out warnings); foreach (Stream stream in streams) stream.Position = 0; if (streams == null || streams.Count == 0) throw new Exception("Error: no stream to print."); var printDocument = new PrintDocument(); printDocument.DefaultPageSettings = pageSettings; if (!printDocument.PrinterSettings.IsValid) throw new Exception("Error: cannot find the default printer."); else { printDocument.PrintPage += (sender, e) => { Metafile pageImage = new Metafile(streams[currentPageIndex]); Rectangle adjustedRect = new Rectangle( e.PageBounds.Left - (int)e.PageSettings.HardMarginX, e.PageBounds.Top - (int)e.PageSettings.HardMarginY, e.PageBounds.Width, e.PageBounds.Height); e.Graphics.FillRectangle(Brushes.White, adjustedRect); e.Graphics.DrawImage(pageImage, adjustedRect); currentPageIndex++; e.HasMorePages = (currentPageIndex < streams.Count); e.Graphics.DrawRectangle(Pens.Red, adjustedRect); }; printDocument.EndPrint += (Sender, e) => { if (streams != null) { foreach (Stream stream in streams) stream.Close(); streams = null; } }; printDocument.Print(); } } }