c#实现的打印DataGrid中的数据--可分页

来源:互联网 发布:linux查看jvm参数设置 编辑:程序博客网 时间:2024/06/07 17:46
我是在vs2003.net里做的,所以有些控件布置什么的就不做说明,和一般的打印程序一样,都只有那么几个.具体的名字什么的,可以根据我的定义名称来判断,比如,dlgPrint就是一个打印控件等。  
   
  using   System;  
  using   System.IO;  
  using   System.Drawing;  
  using   System.Drawing.Printing;  
  using   System.Data;  
  using   System.Windows.Forms;  
   
  //这里隐去公司内部的项目类空间  
   
  namespace   *.*.Manage//这里隐去公司内部的项目类空间  
  {  
    ///   <summary>  
    ///    
    ///   </summary>  
    public   class   frmCourseTable   :   System.Windows.Forms.Form  
    {  
      private   System.Windows.Forms.ToolBar   toolBar1;  
      private   System.Windows.Forms.ImageList   imageList1;  
      private   System.Windows.Forms.ToolBarButton   btnPreView;  
      private   System.Windows.Forms.ToolBarButton   btnPrint;  
      private   System.Windows.Forms.ToolBarButton   btnSave;  
      private   System.Windows.Forms.ToolBarButton   btnClose;  
      private   System.Windows.Forms.ToolBarButton   btnSeparator2;  
      private   System.Windows.Forms.ToolBarButton   btnSeparator1;  
      private   System.Windows.Forms.DataGrid   dgClasses;  
      private   System.Windows.Forms.DataGridTableStyle   dataGridTableStyle1;  
      private   System.Windows.Forms.DataGridTextBoxColumn   dataGridTextBoxColumn1;  
      private   System.Windows.Forms.DataGridTextBoxColumn   dataGridTextBoxColumn2;  
      private   System.Windows.Forms.DataGridTextBoxColumn   dataGridTextBoxColumn3;  
      private   System.Windows.Forms.DataGridTextBoxColumn   dataGridTextBoxColumn4;  
      private   System.Windows.Forms.DataGridTextBoxColumn   dataGridTextBoxColumn5;  
      private   System.Windows.Forms.DataGridTextBoxColumn   dataGridTextBoxColumn6;  
      private   System.Windows.Forms.DataGridTextBoxColumn   dataGridTextBoxColumn7;  
      private   System.Windows.Forms.DataGridTextBoxColumn   dataGridTextBoxColumn8;  
      private   System.Windows.Forms.DataGridTextBoxColumn   dataGridTextBoxColumn9;  
       
      private   System.ComponentModel.IContainer   components   =   null;  
      private   System.Windows.Forms.PrintPreviewDialog   dlgPreview;  
      private   System.Drawing.Printing.PrintDocument   printCourseDoc;  
      private   System.Windows.Forms.ToolBarButton   btnPrintSetup;  
      private   System.Windows.Forms.PageSetupDialog   dlgPrintSetup;  
      private   System.Windows.Forms.PrintDialog   dlgPrint;  
      private   System.Windows.Forms.PrintPreviewDialog   printPreviewDialog1;  
   
      private   string   fileName   =   "未命名";  
      private   DataTable   dgTable;  
      private   int   ColsCount;//列数  
      private   int   PrintRecordComplete   =   0;//已经打印完的记录行数  
      private   int   PrintingPageNumber   =   0;//正要打印的页,初始为0  
      private   int   printLines;//当前页共要分成多少行,其值由“纵向打印”还是“横向打印”来决定  
                      //是针对整个页面来说的,不管是否有内容。该常量只用于划分单元格,不做打印之用  
   
      private   int   PrintRecordNumber   =   45;//每页要打印的记录的行数,初始设定  
                            //针对每页要打印的记录的行数来说的,注意与pringLines的区别,一般来说该常量数值应小于printLines  
   
      private   int   PageRecordNumber   =   0;//当前要打印的行数,由计算得到  
      private   int   PrintingLine   =   0;//当前正在打印的行数  
      private   PointF   DrawPoint;//(标题的)位置在顶部的正中央  
      private   Brush   DrawBrush   =   Brushes.Black;//画笔颜色  
      int   x;  
      int   y;  
      int   X_unit;  
      int   Y_unit;  
   
      public   frmCourseTable()  
      {  
        InitializeComponent();  
      }  
   
      ///   <summary>  
      ///   清理所有正在使用的资源。  
      ///   </summary>  
      protected   override   void   Dispose(   bool   disposing   )  
      {  
        if(   disposing   )  
        {  
          if(components   !=   null)  
          {  
            components.Dispose();  
          }  
        }  
        base.Dispose(   disposing   );  
      }  
   
      private   void   frmCourseTable_Load(object   sender,   System.EventArgs   e)  
      {  
        try  
        {  
          Class   cla   =   new   Class();//这里是公司内部的项目类  
          dgTable   =   cla.ListAllClasses();  
          dgTable.TableName   =   "Result";  
          dgClasses.DataSource   =   dgTable;  
          dgClasses.CaptionText   =   "课程列表";  
        }  
        catch(Exception   ex)  
        {  
          Common.ShowErrorDetail(ex.Message);  
        }  
      }  
   
      #region   Windows   窗体设计器生成的代码  
      ///   <summary>  
      ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
      ///   此方法的内容。  
      ///   </summary>  
      private   void   InitializeComponent()  
      {  
        this.components   =   new   System.ComponentModel.Container();  
        System.Resources.ResourceManager   resources   =   new   System.Resources.ResourceManager(typeof(frmCourseTable));  
        this.toolBar1   =   new   System.Windows.Forms.ToolBar();  
        this.btnPreView   =   new   System.Windows.Forms.ToolBarButton();  
        this.btnPrintSetup   =   new   System.Windows.Forms.ToolBarButton();  
        this.btnPrint   =   new   System.Windows.Forms.ToolBarButton();  
        this.btnSeparator1   =   new   System.Windows.Forms.ToolBarButton();  
        this.btnSave   =   new   System.Windows.Forms.ToolBarButton();  
        this.btnSeparator2   =   new   System.Windows.Forms.ToolBarButton();  
        this.btnClose   =   new   System.Windows.Forms.ToolBarButton();  
        this.imageList1   =   new   System.Windows.Forms.ImageList(this.components);  
        this.dgClasses   =   new   System.Windows.Forms.DataGrid();  
        this.dataGridTableStyle1   =   new   System.Windows.Forms.DataGridTableStyle();  
        this.dataGridTextBoxColumn1   =   new   System.Windows.Forms.DataGridTextBoxColumn();  
        this.dataGridTextBoxColumn2   =   new   System.Windows.Forms.DataGridTextBoxColumn();  
        this.dataGridTextBoxColumn3   =   new   System.Windows.Forms.DataGridTextBoxColumn();  
        this.dataGridTextBoxColumn4   =   new   System.Windows.Forms.DataGridTextBoxColumn();  
        this.dataGridTextBoxColumn5   =   new   System.Windows.Forms.DataGridTextBoxColumn();  
        this.dataGridTextBoxColumn6   =   new   System.Windows.Forms.DataGridTextBoxColumn();  
        this.dataGridTextBoxColumn7   =   new   System.Windows.Forms.DataGridTextBoxColumn();  
        this.dataGridTextBoxColumn8   =   new   System.Windows.Forms.DataGridTextBoxColumn();  
        this.dataGridTextBoxColumn9   =   new   System.Windows.Forms.DataGridTextBoxColumn();  
        this.dlgPreview   =   new   System.Windows.Forms.PrintPreviewDialog();  
        this.printCourseDoc   =   new   System.Drawing.Printing.PrintDocument();  
        this.dlgPrintSetup   =   new   System.Windows.Forms.PageSetupDialog();  
        this.dlgPrint   =   new   System.Windows.Forms.PrintDialog();  
        this.printPreviewDialog1   =   new   System.Windows.Forms.PrintPreviewDialog();  
        ((System.ComponentModel.ISupportInitialize)(this.dgClasses)).BeginInit();  
        this.SuspendLayout();  
        //    
        //   toolBar1  
        //    
        this.toolBar1.AutoSize   =   false;  
        this.toolBar1.Buttons.AddRange(new   System.Windows.Forms.ToolBarButton[]   {  
                                              this.btnPreView,  
                                              this.btnPrintSetup,  
                                              this.btnPrint,  
                                              this.btnSeparator1,  
                                              this.btnSave,  
                                              this.btnSeparator2,  
                                              this.btnClose});  
        this.toolBar1.DropDownArrows   =   true;  
        this.toolBar1.Font   =   new   System.Drawing.Font("宋体",   9F,   System.Drawing.FontStyle.Regular,   System.Drawing.GraphicsUnit.Point,   ((System.Byte)(134)));  
        this.toolBar1.ImageList   =   this.imageList1;  
        this.toolBar1.Location   =   new   System.Drawing.Point(0,   0);  
        this.toolBar1.Name   =   "toolBar1";  
        this.toolBar1.ShowToolTips   =   true;  
        this.toolBar1.Size   =   new   System.Drawing.Size(416,   32);  
        this.toolBar1.TabIndex   =   0;  
        this.toolBar1.TextAlign   =   System.Windows.Forms.ToolBarTextAlign.Right;  
        this.toolBar1.ButtonClick   +=   new   System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);   
  //   btnPreView  
        //    
        this.btnPreView.ImageIndex   =   0;  
        this.btnPreView.ToolTipText   =   "打印预览";  
        //    
        //   btnPrintSetup  
        //    
        this.btnPrintSetup.ImageIndex   =   3;  
        this.btnPrintSetup.ToolTipText   =   "打印设置";  
        //    
        //   btnPrint  
        //    
        this.btnPrint.ImageIndex   =   1;  
        this.btnPrint.ToolTipText   =   "打印";  
        //    
        //   btnSeparator1  
        //    
        this.btnSeparator1.Style   =   System.Windows.Forms.ToolBarButtonStyle.Separator;  
        //    
        //   btnSave  
        //    
        this.btnSave.ImageIndex   =   2;  
        this.btnSave.ToolTipText   =   "保存";  
        //    
        //   btnSeparator2  
        //    
        this.btnSeparator2.Style   =   System.Windows.Forms.ToolBarButtonStyle.Separator;  
        //    
        //   btnClose  
        //    
        this.btnClose.Text   =   "关闭";  
        this.btnClose.ToolTipText   =   "关闭";  
        //    
        //   imageList1  
        //    
        this.imageList1.ImageSize   =   new   System.Drawing.Size(16,   16);  
        this.imageList1.ImageStream   =   ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));  
        this.imageList1.TransparentColor   =   System.Drawing.Color.Transparent;  
        //    
        //   dgClasses  
        //    
        this.dgClasses.CaptionVisible   =   false;  
        this.dgClasses.DataMember   =   "";  
        this.dgClasses.Dock   =   System.Windows.Forms.DockStyle.Fill;  
        this.dgClasses.HeaderForeColor   =   System.Drawing.SystemColors.ControlText;  
        this.dgClasses.Location   =   new   System.Drawing.Point(0,   32);  
        this.dgClasses.Name   =   "dgClasses";  
        this.dgClasses.ReadOnly   =   true;  
        this.dgClasses.Size   =   new   System.Drawing.Size(416,   261);  
        this.dgClasses.TabIndex   =   1;  
        this.dgClasses.TableStyles.AddRange(new   System.Windows.Forms.DataGridTableStyle[]   {  
                                                      this.dataGridTableStyle1});  
        //    
        //   dataGridTableStyle1  
        //    
        this.dataGridTableStyle1.DataGrid   =   this.dgClasses;  
        this.dataGridTableStyle1.GridColumnStyles.AddRange(new   System.Windows.Forms.DataGridColumnStyle[]   {  
                                                              this.dataGridTextBoxColumn1,  
                                                              this.dataGridTextBoxColumn2,  
                                                              this.dataGridTextBoxColumn3,  
                                                              this.dataGridTextBoxColumn4,  
                                                              this.dataGridTextBoxColumn5,  
                                                              this.dataGridTextBoxColumn6,  
                                                              this.dataGridTextBoxColumn7,  
                                                              this.dataGridTextBoxColumn8,  
                                                              this.dataGridTextBoxColumn9});  
        this.dataGridTableStyle1.HeaderForeColor   =   System.Drawing.SystemColors.ControlText;  
        this.dataGridTableStyle1.MappingName   =   "Result";  
        this.dataGridTableStyle1.ReadOnly   =   true;  
        //    
        //   dataGridTextBoxColumn1  
        //    
        this.dataGridTextBoxColumn1.Format   =   "";  
        this.dataGridTextBoxColumn1.FormatInfo   =   null;  
        this.dataGridTextBoxColumn1.HeaderText   =   "课程号";  
        this.dataGridTextBoxColumn1.MappingName   =   "ID";  
        this.dataGridTextBoxColumn1.Width   =   55;  
        //    
        //   dataGridTextBoxColumn2  
        //    
        this.dataGridTextBoxColumn2.Format   =   "";  
        this.dataGridTextBoxColumn2.FormatInfo   =   null;  
        this.dataGridTextBoxColumn2.HeaderText   =   "教师号";  
        this.dataGridTextBoxColumn2.MappingName   =   "TeachNo";  
        this.dataGridTextBoxColumn2.Width   =   40;  
        //    
        //   dataGridTextBoxColumn3  
        //    
        this.dataGridTextBoxColumn3.Format   =   "";  
        this.dataGridTextBoxColumn3.FormatInfo   =   null;  
        this.dataGridTextBoxColumn3.HeaderText   =   "课程名称";  
        this.dataGridTextBoxColumn3.MappingName   =   "Name";  
        this.dataGridTextBoxColumn3.Width   =   150;  
        //    
        //   dataGridTextBoxColumn4  
        //    
        this.dataGridTextBoxColumn4.Format   =   "";  
        this.dataGridTextBoxColumn4.FormatInfo   =   null;  
        this.dataGridTextBoxColumn4.HeaderText   =   "教师名称";  
        this.dataGridTextBoxColumn4.MappingName   =   "TeachName";  
        this.dataGridTextBoxColumn4.Width   =   90;  
        //    
        //   dataGridTextBoxColumn5  
        //    
        this.dataGridTextBoxColumn5.Format   =   "";  
        this.dataGridTextBoxColumn5.FormatInfo   =   null;  
        this.dataGridTextBoxColumn5.HeaderText   =   "学分";  
        this.dataGridTextBoxColumn5.MappingName   =   "Credit";  
        this.dataGridTextBoxColumn5.Width   =   10;  
        //    
        //   dataGridTextBoxColumn6  
        //    
        this.dataGridTextBoxColumn6.Format   =   "";  
        this.dataGridTextBoxColumn6.FormatInfo   =   null;  
        this.dataGridTextBoxColumn6.HeaderText   =   "实际人数";  
        this.dataGridTextBoxColumn6.MappingName   =   "RealNum";  
        this.dataGridTextBoxColumn6.Width   =   10;  
        //    
        //   dataGridTextBoxColumn7  
        //    
        this.dataGridTextBoxColumn7.Format   =   "";  
        this.dataGridTextBoxColumn7.FormatInfo   =   null;  
        this.dataGridTextBoxColumn7.HeaderText   =   "额定人数";  
        this.dataGridTextBoxColumn7.MappingName   =   "TOTALNUM";  
        this.dataGridTextBoxColumn7.Width   =   10;  
        //    
        //   dataGridTextBoxColumn8  
        //    
        this.dataGridTextBoxColumn8.Format   =   "";  
        this.dataGridTextBoxColumn8.FormatInfo   =   null;  
        this.dataGridTextBoxColumn8.HeaderText   =   "上课时间";  
        this.dataGridTextBoxColumn8.MappingName   =   "TimeText";  
        this.dataGridTextBoxColumn8.Width   =   120;  
        //    
        //   dataGridTextBoxColumn9  
        //    
        this.dataGridTextBoxColumn9.Format   =   "";  
        this.dataGridTextBoxColumn9.FormatInfo   =   null;  
        this.dataGridTextBoxColumn9.HeaderText   =   "上课地点";  
        this.dataGridTextBoxColumn9.MappingName   =   "Room";  
        this.dataGridTextBoxColumn9.Width   =   120;  
        //    
        //   dlgPreview  
        //    
        this.dlgPreview.AutoScrollMargin   =   new   System.Drawing.Size(0,   0);  
        this.dlgPreview.AutoScrollMinSize   =   new   System.Drawing.Size(0,   0);  
        this.dlgPreview.ClientSize   =   new   System.Drawing.Size(400,   300);  
        this.dlgPreview.Document   =   this.printCourseDoc;  
        this.dlgPreview.Enabled   =   true;  
        this.dlgPreview.Icon   =   ((System.Drawing.Icon)(resources.GetObject("dlgPreview.Icon")));  
        this.dlgPreview.Location   =   new   System.Drawing.Point(230,   20);  
        this.dlgPreview.MinimumSize   =   new   System.Drawing.Size(375,   250);  
        this.dlgPreview.Name   =   "dlgPreview";  
        this.dlgPreview.TransparencyKey   =   System.Drawing.Color.Empty;  
        this.dlgPreview.Visible   =   false;  
        //    
        //   printCourseDoc  
        //    
        this.printCourseDoc.BeginPrint   +=   new   System.Drawing.Printing.PrintEventHandler(this.printCourseDoc_BeginPrint);  
        this.printCourseDoc.PrintPage   +=   new   System.Drawing.Printing.PrintPageEventHandler(this.printCourseDoc_PrintPage);  
        //    
        //   dlgPrintSetup  
        //    
        this.dlgPrintSetup.Document   =   this.printCourseDoc;  
        //    
        //   dlgPrint  
        //    
        this.dlgPrint.Document   =   this.printCourseDoc;  
        //    
        //   printPreviewDialog1  
        //    
        this.printPreviewDialog1.AutoScrollMargin   =   new   System.Drawing.Size(0,   0);  
        this.printPreviewDialog1.AutoScrollMinSize   =   new   System.Drawing.Size(0,   0);  
        this.printPreviewDialog1.ClientSize   =   new   System.Drawing.Size(400,   300);  
        this.printPreviewDialog1.Enabled   =   true;  
        this.printPreviewDialog1.Icon   =   ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));  
        this.printPreviewDialog1.Location   =   new   System.Drawing.Point(176,   176);  
        this.printPreviewDialog1.MinimumSize   =   new   System.Drawing.Size(375,   250);  
        this.printPreviewDialog1.Name   =   "printPreviewDialog1";  
        this.printPreviewDialog1.TransparencyKey   =   System.Drawing.Color.Empty;  
        this.printPreviewDialog1.Visible   =   false;  
        //    
        //   frmCourseTable  
        //    
        this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);  
        this.ClientSize   =   new   System.Drawing.Size(416,   293);  
        this.Controls.Add(this.dgClasses);  
        this.Controls.Add(this.toolBar1);  
        this.Name   =   "frmCourseTable";  
        this.StartPosition   =   System.Windows.Forms.FormStartPosition.CenterParent;  
        this.Text   =   "课程列表";  
        this.Load   +=   new   System.EventHandler(this.frmCourseTable_Load);  
        ((System.ComponentModel.ISupportInitialize)(this.dgClasses)).EndInit();  
        this.ResumeLayout(false);  
   
      }  
      #endregion  
private   void   toolBar1_ButtonClick(object   sender,   System.Windows.Forms.ToolBarButtonClickEventArgs   e)  
      {  
        try  
        {  
          if(e.Button   ==   btnPreView)  
          {  
            dlgPreview.ShowDialog();//打印预览  
          }  
          if(e.Button   ==   btnPrintSetup)  
          {  
            dlgPrintSetup.ShowDialog();//打印设置  
          }  
          if(e.Button   ==   btnPrint)  
          {  
            if(dlgPrint.ShowDialog()   ==   DialogResult.OK)  
            {  
              printCourseDoc.Print();//打印  
            }  
          }  
          if(e.Button   ==   btnSave)  
          {  
            SaveFileDlg();//保存  
          }  
          if(e.Button   ==   btnClose)  
          {  
            this.Close();//关闭  
          }  
        }  
        catch(Exception   ex)  
        {  
          MessageBox.Show(ex.Message,   "打印出现错误",  
            MessageBoxButtons.OK,   MessageBoxIcon.Exclamation);  
        }  
      }  
   
      protected   void   SaveFileDlg()  
      {//这里保存文件的代码未完成  
      }  
   
      protected   void   SaveFile()  
      {//这里保存文件的代码未完成  
      }  
   
      //从“打印设置PrintSetup”中获取相关参数,进行打印  
      //这里有个问题:如果选择纸张大小为“自定义大小”,那么在PrintPreview的时候就会出错,为什么呢?  
      private   void   printCourseDoc_PrintPage(object   sender,   System.Drawing.Printing.PrintPageEventArgs   e)  
      {  
        int   Cols   =   0;  
        x   =   e.MarginBounds.Left;//纸张可写处的最左边  
        y   =   e.MarginBounds.Top;//纸张可写处的最上端  
   
        IsFullPage(dgTable,   PrintingPageNumber,   PrintRecordNumber);        
               
        int   columnCounts   =   0;//定义DataGrid中的列数  
        string[]   colText   =   new   string[columnCounts];//定义一个与DataGrid中的列数相同大小的数组,存放其中的字段名称  
        string[]   colID   =   new   string[columnCounts];//定义一个存放字段属性值(如ID,TeacherName)等的数组,打印DataGrid中的数据时要用到  
        int[]   colSize   =   new   int[columnCounts];//定义一个存放各个字段大小的数组  
   
        //取得DataGrid中的列数,以及各个列的名称,并赋给ColText数组  
        foreach(DataGridTableStyle   myGridStyle   in   dgClasses.TableStyles)  
        {  
          string[]   headerText   =   new   string[myGridStyle.GridColumnStyles.Count];  
          string[]   headerID   =   new   string[myGridStyle.GridColumnStyles.Count];  
          int[]   columnSize   =   new   int[myGridStyle.GridColumnStyles.Count];  
   
          //遍历dgClasses中的每个表所有的字段  
          foreach(DataGridColumnStyle   myColumnStyle   in   myGridStyle.GridColumnStyles)  
          {  
            headerText[columnCounts++]   =   myColumnStyle.HeaderText;//获得字段名称,如“学号”等  
            headerID[columnCounts-1]   =   myColumnStyle.MappingName;//获取字段属性值,如“ID”等  
            columnSize[columnCounts-1]   =   myColumnStyle.Width;  
          }  
          colText   =   headerText;//赋给前者  
          colID   =   headerID;//赋给前者  
          colSize   =   columnSize;//赋给前者  
        }  
         
        try  
        {  
          //将当前页分成基本的单元  
          if(printCourseDoc.DefaultPageSettings.Landscape   ==   true)  
          {  
            X_unit   =   e.MarginBounds.Height   /   (columnCounts   +   5);//[横向打印时]除以“要打印的字段总数+5”,得到纵向间隔大小  
            Y_unit   =   e.MarginBounds.Width   /   printLines;  
          }  
          else  
          {  
            X_unit   =   e.MarginBounds.Width   /   (columnCounts   +   5);//[纵向打印]除以“要打印的字段总数+5”,得到横向间隔大小  
            Y_unit   =   e.MarginBounds.Height   /   printLines;  
          }  
        }  
        catch(Exception   ex)  
        {  
          Common.ShowErrorDetail(ex.Message);  
        }  
         
        //打印文档标题,自定义  
        string   strPrintTitle   =   dgClasses.CaptionText;//文档标题,这里为“课程列表”  
        DrawPoint   =   new   PointF(x   +   e.MarginBounds.Width/2   -   50,   y   );//设定标题的打印位置坐标(x,y)=(矩形最左边坐标+矩形的宽度的一半,矩形最上边的坐标)  
        e.Graphics.DrawString(strPrintTitle,   PrintFont("宋体",   20),   DrawBrush,   DrawPoint);//打印文档标题  
        y   +=   Y_unit   *   2;//打印完标题,往下移动两个“Y_unit”单位  
   
        //打印列名  
        for(Cols=0;Cols<columnCounts;Cols++)  
        {  
          if(Cols   ==   0)  
          {  
            DrawPoint.X   =   X_unit;//如果是第一列,则从X_unit开始  
          }  
          else  
          {  
            DrawPoint.X   +=   colSize[Cols-1]   +   X_unit;//如果是第二个以后,则从每次往前移动一个单元格和前一个的字段大小  
          }  
          DrawPoint.Y   =   y;//设定当前列名的打印位置  
                                  e.Graphics.DrawString(colText[Cols],   PrintFont("宋体",   10),   DrawBrush,   DrawPoint);//打印所有列名  
        }  
   
        DrawPoint   =   new   PointF(X_unit,   y);  
        DrawLine(DrawPoint,   e);//在列名下面打印一条黑色直线  
        y   +=   Y_unit;//打印完全部列名,往下移动“Y_unit”个单位  
   
     
        //打印DataGrid中的数据  
        while(PrintingLine<PageRecordNumber)  
        {  
          DataRow   dgRow   =   dgTable.Rows[PrintRecordComplete];  
          IsFullPage(dgTable,   PrintingPageNumber,   PrintRecordNumber);//判断最后一页的记录数是否可以打满一页,否则出错。  
   
          for(Cols=0;Cols<columnCounts;Cols++)  
          {  
            if(Cols   ==   0)  
            {  
              DrawPoint.X   =   X_unit;  
            }  
            else  
            {  
              DrawPoint.X   +=   colSize[Cols-1]   +   X_unit;  
            }  
            DrawPoint.Y   =   y;  
            e.Graphics.DrawString(dgRow[colID[Cols]].ToString(),   PrintFont("宋体",   10),   DrawBrush,   DrawPoint);  
          }  
          y   +=   Y_unit;//打印完一行,就往下移动“Y_unit”个单位  
          PrintingLine   +=   1;//正在打印的行数+1  
          PrintRecordComplete   +=   1;//已经打印的记录数+1  
   
          if(y   >   e.MarginBounds.Bottom)  
          {  
            DrawPoint.X   =   X_unit   *   1;  
            DrawPoint.Y   =   y;  
            DrawLine(DrawPoint,   e);//在每个页面的底尾打印一条黑色直线  
   
            e.HasMorePages   =   true;//如果已经到达纸张底部,则开始新页  
   
            PrintingPageNumber   +=   1;//正要打印的页数+1  
            PageRecordNumber   =   0;//将当前要打印的行数置0  
            PrintingLine   =   0;//将正在打印的行数置0  
            return;  
          }  
        }  
   
        e.HasMorePages   =   false;  
      }  
   
      //只调用一次,用于初始化  
      private   void   printCourseDoc_BeginPrint(object   sender,   System.Drawing.Printing.PrintEventArgs   e)  
      {  
        dgTable   =   (DataTable)dgClasses.DataSource;  
        ColsCount   =   dgTable.Columns.Count;  
   
        //当前页是横向还是纵向打印,并计算当前页总共可以打印的行数  
        if(printCourseDoc.DefaultPageSettings.Landscape   ==   false)  
        {  
          printLines   =   printCourseDoc.DefaultPageSettings.PaperSize.Height   /   (PrintFont("宋体",   10).Height   +   10);//纵向打印的情况  
        }  
        else  
        {  
          printLines   =   printCourseDoc.DefaultPageSettings.PaperSize.Width   /   (PrintFont("宋体",   10).Height   +   10);//横向打印的情况  
        }  
      }  
   
      //计算,余下的记录条数是否还可以在一页打印,不满一页时为假  
      protected   void   IsFullPage(DataTable   dgTable,   int   PrintingPageNumber,   int   PrintRecordNumber)  
      {  
        //计算,余下的记录条数是否还可以在一页打印,不满一页时为假  
        if(dgTable.Rows.Count   -   PrintingPageNumber   *   PrintRecordNumber   >=   PrintRecordNumber)  
        {  
          PageRecordNumber   =   PrintRecordNumber;//余下的条数还可以打满一页,将“每页可打印条数”赋给“当前要打印的行数”  
        }  
        else  
        {  
          PageRecordNumber   =   (dgTable.Rows.Count   -   PrintingPageNumber   *   PrintRecordNumber)   %   PrintRecordNumber;  
        }  
      }  
   
      //Draw   a   line   with   a   black   pen.  
      protected   void   DrawLine(PointF   point,   System.Drawing.Printing.PrintPageEventArgs   e)  
      {  
        Pen   blackPen   =   new   Pen(System.Drawing.Color.Black,   1);  
        e.Graphics.DrawLine(blackPen,   point.X,   point.Y   +   PrintFont("宋体",   10).Height,   point.X   *   ColsCount,   point.Y   +   PrintFont("宋体",   10).Height);  
      }  
   
      //返回字体设置  
      protected   Font   PrintFont(string   font,   int   size)  
      {  
        return   new   Font(font,size);  
      }  
    }  
  }
原创粉丝点击