另一个 DataGridView 打印程序
DataGridView 打印功能封装在一个独立的类中。非常易于使用!已更新,允许打印宽度超过一页的列。
引言
我曾试图寻找一个类来从 DataGridView
中进行打印,但没有一个类能满足我所有的需求。我需要打印所有页面、部分页面或当前选择的内容;并且我需要打印对象、控件或代码不被分散到我代码的其余部分——也就是说,它需要完全独立。我找到的任何东西都不能满足所有这些要求,所以我最终自己编写了一个。
Using the Code
要使用 DGVPrinter
类,您有两个选择。首先,您可以简单地将 DGVPrinter.cs 源文件添加到您的项目中,或者第二,您可以将 DLL 放在您的“Bin”目录中,并将 DGVPrinter.dll 的引用添加到您项目的引用中。无论哪种情况,要使用 DGVPrinter
,您只需在您的代码文件中添加“using DGVPrinter
”,并创建一个该对象的实例。
//
// The using block statement
//
using DGVPrinterHelper;
//
// The using block statement
//
imports DGVPrinterHelper;
例如,如果想在用户单击工具栏上的“打印”按钮时打印 DataGridView
,您的代码可能看起来像这样
//
// Printing the DataGridView Control
// in response to a toolbar button press
//
private void printToolStripButton_Click(object sender, EventArgs e)
{
DGVPrinter printer = new DGVPrinter();
printer.Title = "DataGridView Report";
printer.SubTitle = "An Easy to Use DataGridView Printing Object";
printer.SubTitleFormatFlags = StringFormatFlags.LineLimit |
StringFormatFlags.NoClip;
printer.PageNumbers = true;
printer.PageNumberInHeader = false;
printer.PorportionalColumns = true;
printer.HeaderCellAlignment = StringAlignment.Near;
printer.Footer = "Your Company Name Here";
printer.FooterSpacing = 15;
printer.PrintDataGridView(datagridviewControl);
}
//
// Printing the DataGridView Control
// in response to a toolbar button press
//
Public Class Form1
Private Sub btnPrintGridview_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnPrintGridView.Click
Dim Printer = New DGVPrinter
Printer.Title = "DataGridView Report"
Printer.SubTitle = "An Easy to Use DataGridView Printing Object"
Printer.SubTitleFormatFlags = StringFormatFlags.LineLimit Or _
StringFormatFlags.NoClip
Printer.PageNumbers = True
Printer.PageNumberInHeader = False
Printer.PorportionalColumns = True
Printer.HeaderCellAlignment = StringAlignment.Near
Printer.Footer = "Your Company Name Here"
Printer.FooterSpacing = 15
Printer.PrintDataGridView(Me.DataGridView1)
End Sub
这里提供的基本接口为打印 DataGridView
提供了一个方便的一站式解决方案。但是,如果您想对打印过程有更多控制呢?例如,您可能想保存用户的打印偏好或提供一个默认打印机?为了帮助您解决这个问题,DGVPrinter
提供了一个更复杂的接口。这里有一个示例,其中调用程序提供了 PrinterSettings
和 DefaultPageSettings
的覆盖。
//
// Printing the DataGridView Control
// in response to a toolbar button press –
// the myprintsettings and mypagesettings objects are objects used by the local
// program to save printer and page settings
//
private void printToolStripButton_Click(object sender, EventArgs e)
{
DGVPrinter printer = new DGVPrinter();
printer.Title = "DataGridView Report";
printer.SubTitle = "An Easy to Use DataGridView Printing Object";
printer.SubTitleFormatFlags = StringFormatFlags.LineLimit |
StringFormatFlags.NoClip;
printer.PageNumbers = true;
printer.PageNumberInHeader = false;
printer.PorportionalColumns = true;
printer.HeaderCellAlignment = StringAlignment.Near;
printer.Footer = "Your Company Name Here";
printer.FooterSpacing = 15;
// use saved settings
if (null != myprintsettings)
printer.PrintDocument.PrinterSettings = myprintsettings;
if (null != mypagesettings)
printer.PrintDocument.DefaultPageSettings = mypagesettings;
if (DialogResult.OK == printer.DisplayPrintDialog()) // replace DisplayPrintDialog()
// with your own print dialog
{
// save users' settings
myprintsettings = printer.PrinterSettings;
mypagesettings = printer.PageSettings;
// print without displaying the printdialog
printer.PrintNoDisplay(datagridviewControl);
}
}
DGVPrinter
的各种设置提供了对页面上所有打印方面的良好控制。您可以设置标题和副标题、添加页脚,并控制页码是否打印在页眉或页脚中。DGVPrinter
支持从右到左打印(适用于非西方语言),并包含一个绘图覆盖功能,用于源 DataGridView
控件中单元格或列的 onPaint
被覆盖的情况。虽然打印的 DataGridView
的默认样式取自源 DataGridView
控件,但 DGVPrinter
还提供了许多属性,允许您控制打印输出几乎所有方面的样式。
历史
- 版本 1.0 - 初次发布
- 版本 1.1 - 添加了页脚处理,并允许页码打印在页眉或页脚中,以及是否与页眉或页脚在同一行打印
- 版本 1.2 - 终于(我认为!)修复了字符串/列对齐问题。还能正确打印单元格背景颜色,并保留了交替行样式
- 版本 1.3 - 添加了对打印包含图像的列的支持
- 版本 1.4 - 添加了直接打印到提供的
Graphics
对象的功能 - 版本 2.0 - 添加了在页面上打印图像的支持
- 版本 3.0 - 重大更改!请阅读!
- 添加了对跨越多页的单元格/行的支持。如果一个单元格将超出页面底部,"
KeepRowsTogether
" 属性将决定是打印部分行还是开始新页面。 - 添加了对设置行和列标题样式(Header Styles)的支持。设置标题单元格样式的属性名称已更改,"
PrintColumnHeaders
" 的返回值已更改。这可能会导致您的程序无法编译/运行! - 添加了一个默认值,因此如果行标题被设置为“可见”,它们将显示出来。
- 添加了标题和副标题间距。这些将帮助您控制标题和副标题下方的空白。
- 编译版本以支持 VB 和其他语言
- 添加了对跨越多页的单元格/行的支持。如果一个单元格将超出页面底部,"
- 版本 3.1 - 修复了单元格背景颜色打印问题
- 版本 3.2 - 修复了嵌入式打印功能
- 版本 3.3 - 可能(但不太可能)的重大更改
- 添加委托以允许单元格(包括行和列标题)的“所有者绘制”
- 添加了对单元格大小、数据大小或列比例缩放的更好支持。由于现在已正确计算图像的大小,标识符 '
StringHeight
' 已更改为 'DataHeight
'。如果您依赖此功能,这可能会破坏您的代码。 - Bug 修复
- 版本 3.4 - 在覆盖
ColumnStyles
时,添加了对交替行的支持 - 版本 3.5 - 修复了交替行
ColumnStyles
的更多问题 - 版本 3.6 - 修复了嵌入式图像绘制;图像现在以原始像素大小绘制,无缩放
- 版本 3.7 - 修复了大型文本换行问题
- 版本 4.0 - 修复了许多问题和大量新功能!
- Bug 修复
- 会员 8539779 发现的字体大小调整问题
- 跨越多页的行未正确遵守从右到左的语言设置
- 新功能 - 非常感谢所有建议新功能或特性的用户!!
- 设置标题、副标题和页脚的背景颜色/阴影
- 设置标题、副标题和页脚的边框样式和颜色
- 隐藏列 - 指定将不会打印的列列表
- 固定列 - 指定将在每一页打印的列列表(适用于跨越多页的行)。遵守从右到左的语言设置
- 按值更改中断 - 指定要监视的列。当此列中单元格的值发生更改时,将插入一个分页符。
- 复选框列现在显示为实际的图形复选框。
- Bug 修复
- 版本 4.1
- 大大扩展了嵌入式打印过程,现在支持多页并公开了 BeginPrint 和 PrintPage 事件。
- 添加了日志/跟踪设施
- 错误修复 - 感谢 B. Marik 的耐心和帮助,在我跟踪到这个问题时。
- 版本 4.2
- 错误修复 - 打印不再抛出错误。感谢会员 8757586 为我找到它!
- 版本 4.3
- 错误修复 - 改进了复选框的处理,增加了对三态复选框的支持。
- 错误修复 - 如果隐藏了第一列,则列标题将不会打印
- 版本 4.4(感谢 Derek 发现并帮助我找到和修复这些问题!)
- 错误修复 - 处理包含大量数据的单元格。
- 错误修复 - 在某些情况下,页脚会打印在文本的最后一行之上。
感谢、赞扬和致谢所有提出功能或特性的用户,以及找到bug的用户。没有大家的想法和贡献,DGV Printer 就不会是现在的样子!