在打印按钮的点击事件中调用下面的方法:
//printPreviewDialog1打印,可预览打印内容
private void Print1()
{
if (System.Windows.Forms.MessageBox.Show("是否要预览打印文档", "打印预览",System.Windows.Forms.MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.printPreviewDialog1.UseAntiAlias = true;
this.printPreviewDialog1.Document = this.printDocument1;
printer1 = new DataGridViewPrinter(dataGridView1, printDocument1, true, true,
"表头1", "表头2", new Font("宋体", 30, FontStyle.Regular),
new Font("宋体", 30, FontStyle.Regular), Color.Black);
printPreviewDialog1.ShowDialog();
}
else
{
this.printDocument1.Print();//不预览,直接打印
}
}
printDocument1的PrintPage事件处理程序:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
//e.HasMorePage为真,系统会自动调用printDocument1_PrintPage方法。实现在预览时的分页
if (printer1.DrawDataGridView(e.Graphics))
e.HasMorePages = true;
else
e.HasMorePages = false;
}
自定义类,绘制打印内容(以网上的代码为基础,做了一些修改,添加了一些解释,想看原来的代码请点击这里!):
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.ComponentModel;
namespace v2.print
{
class DataGridViewPrinter
{
//定义成员变量
private DataGridView TheDataGridView;
private PrintDocument ThePrintDocument;
private bool IsCenterOnPage;
private bool IsWithTitle;
private string TheTitleText;
private string TheTitleTextTwo;
private Font TheTitleFont;
private Font TheTitleFontTwo;
private Color TheTitleColor;
static int CurrentRow;
static int PageNumber;
static int PageNumberson;
static int lastnumber;
private int PageWidth;
private int PageHeight;
private int LeftMargin;
private int TopMargin;
private int RightMargin;
private int BottomMargin;
private float CurrentY;
private float RowHeaderHeight;
private List<float> RowsHeight;
private List<float> ColumnsWidth;
private float TheDataGridViewWidth;
private List<int[]> mColumnPoints;
private List<float> mColumnPointsWidth;
private int mColumnPoint;
//构造函数
public DataGridViewPrinter(DataGridView aDataGridView, PrintDocument aPrintDocument,
bool CenterOnPage, bool WithTitle, string aTitleText,
string bTitleText, Font aTitleFont, Font bTitleFont,
Color aTitleColor)
{
TheDataGridView = aDataGridView;
ThePrintDocument = aPrintDocument;
IsCenterOnPage = CenterOnPage;
IsWithTitle = WithTitle;
TheTitleText = aTitleText;
TheTitleTextTwo = bTitleText;
TheTitleFont = aTitleFont;
TheTitleFontTwo = bTitleFont;
TheTitleColor = aTitleColor;
PageNumber = 0;
PageNumberson = 0;
RowsHeight = new List<float>();
ColumnsWidth = new List<float>();
mColumnPoints = new List<int[]>();
mColumnPointsWidth = new List<float>();
//利用ThePrintDocument.DefaultPageSettings.PaperSize获取页面的宽度和高度
if (!ThePrintDocument.DefaultPageSettings.Landscape)
{
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
}
else
{
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
}
// 计算页边距
LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;
CurrentRow = 0;
lastnumber = 0;
}
//构造函数,初始化变量
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private float intergered(float num)
{
float num_interger;
int point = 0;//小数点的位置
string num_temp = num.ToString();
for (int i = 0; i <