本文档是参考网上的资料,稍加修改,经过实际编译,可实现Winform上dataGridView控件的打印,并实现分页,下面为程序代码,仅供参考。本程序不需要在界面上添加任何打印相关控件。
public partial class Example : Form
{
//打印文檔
PrintDocument pdDocument = new PrintDocument();
//打印格式設置頁面
PageSetupDialog dlgPageSetup = new PageSetupDialog();
//打印頁面
PrintDialog dlgPrint = new PrintDialog();
//實例化打印預覽
PrintPreviewDialog dlgPrintPreview = new PrintPreviewDialog();
public Example()
{
InitializeComponent();
pdDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
//頁面設置的打印文檔設置為需要打印的文檔
dlgPageSetup.Document = pdDocument;
//打印界面的打印文檔設置為被打印文檔
dlgPrint.Document = pdDocument;
//打印預覽的打印文檔設置為被打印文檔
printform dlgPrintPreview.Document = pdDocument;
}
/// <summary>
/// //顯示打印預覽界面 ,此处需要添加一个打印预览的按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrintView_Click(object sender, EventArgs e)
{
dlgPrintPreview.ShowDialog();
}
/// <summary>
/// 打印设置,此处需要添加一个打印设置的按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrintSetup_Click(object sender, EventArgs e)
{
dlgPageSetup.ShowDialog();
dlgPrint.ShowDialog();
}
///
/// printDocument的PrintPage事件 ,实现打印功能
///
///
///
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
int iX = 60;
int iY = 40;
PrintDataGridView11.Print(dataGridView1, true, e, ref iX, ref iY);
}
/// <summary>
/// 打印,此处需添加一个打印按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrint_Click(object sender, EventArgs e)
{
pdDocument.Print();
}
}
/// <summary>
/// 实现DataGridView的打印 类
/// </summary>
public class PrintDataGridView11
{
private static List<DataGridViewCellPrint> CellPrintList = new List<DataGridViewCellPrint>();
/// <summary>
/// 打印的行数
/// </summary>
private static int printRowCount = 0;
/// <summary>
/// 是否要打印
/// </summary>
private static bool IsPrint = true;
/// <summary>
/// 设置的起始位置是否大于默认打印的边框
/// </summary>
private static bool IsRole = true;
/// <summary>
/// X坐标
/// </summary>
private static int PoXTmp = 0;
/// <summary>
/// Y坐标
/// </summary>
private static int PoYTmp = 0;
/// <summary>
/// 列间距
/// </summary>
private static int WidthTmp = 0;
/// <summary>
/// 行间距
/// </summary>
private static int HeightTmp = 0;
/// <summary>
/// 列数
/// </summary>
private static int RowIndex = 0;
///
/// 打印DataGridView控件
///
/// DataGridView控件
/// 是否包括列标题
/// 为 System.Drawing.Printing.PrintDocument.PrintPage 事件提供数据。
/// 起始X坐标
/// 起始Y坐标
///
public static void Print(DataGridView dataGridView, bool includeColumnText, PrintPageEventArgs eValue, ref int PoX, ref int PoY)
{
try
{
if (PrintDataGridView11.IsPrint)
{
PrintDataGridView11.printRowCount = 0;
PrintDataGridView11.IsPrint = false;
PrintDataGridView11.DataGridViewCellVsList(dataGridView, includeColumnText); //获取要打印的数据
if (0 == PrintDataGridView11.CellPrintList.Count)
return;
if (PoX > eValue.MarginBounds.Left) //如果设置的起始位置大于默认打印的边框,IsRole为true
PrintDataGridView11.IsRole = true;
else
PrintDataGridView11.IsRole = false;
PrintDataGridView11.PoXTmp = PoX;
PrintDataGridView11.PoYTmp = PoY;
PrintDataGridView11.RowIndex = 0;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论