C#_实现Winform控件dataGridView的打印与分页
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本文档是参考网上的资料,稍加修改,经过实际编译,可实现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;
//打印預覽的打印文檔設置為被打印文檔
dlgPrintPreview.Document = pdDocument;
}
///<summary>
/// //顯示打印預覽界面,此处需要添加一个打印预览的按钮
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
private void btnPrintView_Click(object sender, EventArgs e)
{
dlgPrintPreview.ShowDialog();
}
///打印设置,此处需要添加一个打印设置的按钮
///</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>
///打印的行数
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