大哥给俺的一个方法(网上的)跟大家分享一下, 我想大哥不会介意的偶
一个DataGrid的打印类- -
namespace AsterDnet { using System; using System.ComponentModel; using System.Windows.Forms; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Data ; using System.Data.SqlClient ; /// <summary> /// 打印DataGrid中的数据到打印机 /// /// </summary> public class DataGridPrintDocument : PrintDocument { //打印字体 private Font printFont = null; private DataTable dt=null;
//当前页码 private int currentPageNumber=1; //是否打印页码 private bool isPrintPageNumber=false; public bool IsPrintPageNumber { get { return this.isPrintPageNumber ; } set { this.isPrintPageNumber =value; } } //表格文字对齐方式:0:居左;1:居中;2:居右 private int ColStyle=0; public int TableColStyle { get { return this.ColStyle; } set { this.ColStyle=value; } } //是否需要竖线 private bool isNeedVertLine=false; public bool IsNeedVertLine { get { return this.isNeedVertLine; } set { this.isNeedVertLine=value; } } //是否需要横线 private bool isNeedHorLine=false; public bool IsNeedHorLine { get { return this.isNeedHorLine; } set { this.isNeedHorLine=value; } } //表体中文字和表格线的间隔比例 private float LineEmpty=1.3F; public float LineEmptyCharacter { get { return this.LineEmpty; } set { this.LineEmpty=value; } } //指定每页打印表格行数,如果超过纸张的高度就取默认值 private int LinePerPage=0; public int LinePerNumberPage { get { return this.LinePerPage ; } set { this.LinePerPage=value; } } //当前行号 private int currentLineNumber=0; //表格列标题 private string[] colname=null; //标题一 private string headtitle1=null; public string HeadTitle1 { get { return this.headtitle1; } set { this.headtitle1=value; } } //标题二 private string headtitle2=null; public string HeadTitle2 { get { return this.headtitle2; } set { this.headtitle2=value; } } //标题三 private string headtitle3=null; public string HeadTitle3 { get { return this.headtitle3; } set { this.headtitle3=value; } } //标题四一 private string headtitle41=null; public string HeadTitle41 { get { return this.headtitle41; } set { this.headtitle41=value; } } //标题四二 private string headtitle42=null; public string HeadTitle42 { get { return this.headtitle42; } set { this.headtitle42=value; } } //标题四三 private string headtitle43=null; public string HeadTitle43 { get { return this.headtitle43; } set { this.headtitle43=value; } } //页尾一一 private string tailtitle11=null; public string TailTitle11 { get { return this.tailtitle11 ; } set { this.tailtitle11 =value; } } //页尾一二 private string tailtitle12=null; public string TailTitle12 { get { return this.tailtitle12; } set { this.tailtitle12 =value; } } //页尾一三 private string tailtitle13=null; public string TailTitle13 { get { return this.tailtitle13 ; } set { this.tailtitle13=value; } } //页尾二 private string tailtitle2=null; public string TailTitle2 { get { return this.tailtitle2; } set { this.tailtitle2 =value; } } //每页打印DataGrid中数据行数 private int numsPerPage; //要打印的DataGrid控件 private DataGrid printDataGrid =null; public DataGrid PrintDataGrid { get { return this.printDataGrid ; } set { this.printDataGrid =value; } } private void LoadTestDB() { } /// <summary> /// 构造器一 /// </summary> public DataGridPrintDocument(DataGrid dataGrid,string[] ColName) { this.printDataGrid = dataGrid ; this.printFont = dataGrid.Font; this.colname =ColName; dt=(DataTable)this.printDataGrid.DataSource; //LoadTestDB(); }
/// <summary> /// 构造器二 /// </summary> /// <param name="dataGrid">要打印的DataGrid控件</param> public DataGridPrintDocument(DataGrid dataGrid) { this.printDataGrid = dataGrid ; this.printFont = dataGrid.Font; }
//Override OnBeginPrint to set up the font we are going to use protected override void OnBeginPrint(PrintEventArgs ev) { base.OnBeginPrint(ev) ;
}