程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 列車時刻查詢系統(北大青鳥大二寒假作業) 源代碼(五)

列車時刻查詢系統(北大青鳥大二寒假作業) 源代碼(五)

編輯:.NET實例教程

打印,打印datagrid

 



using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Data;

namespace train
...{
    /**//// <summary>
    /// DataGridPrinter負責打印
    /// </summary>
    public class DataGridPrinter
    ...{

        private DataTable table;
        private PrintDocument printDocument;
        private PageSetupDialog pageSetupDialog;
        private PrintPreviewDialog printPrevIEwDialog;

        public DataGridPrinter(DataTable printtable)
        ...{

 

    printDocument=new PrintDocument();

            table=printtable;
        }


        /**//// <summary>
        /// 實現打印事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument_PrintPage(object sender,PrintPageEventArgs e)
        ...{
            Font objFont=new Font("Tahoma",12,FontStyle.Regular);
            Brush objBrush=Brushes.Black;
            Pen objPen=new Pen(objBrush);
            objPen.Width=3;
            float nleft=pageSetupDialog.PageSettings.Margins.Left;
            float ntop=pageSetupDialog.PageSettings.Margins.Top;
            for(int i=0;i<table.Columns.Count;i++)
            ...{

  e.Graphics.DrawString(table.Columns[i].ColumnName,objFont,objBrush,nleft,ntop);

                nleft=nleft+180;
            }
            ntop=ntop+20;
            try
            ...{    
                for(int i=0;i<table.Rows.Count;i++)
                ...{
                    ntop=ntop+20;
                    nleft=pageSetupDialog.PageSettings.Margins.Left;
                    for(int j=0;j<table.Columns.Count;j++)
                    ...{
                        e.Graphics.DrawString(table.Rows[i][j].ToString(),objFont,objBrush,nleft,ntop);
                        nleft=nleft+180;
                    }
                }
            }
  catch(Exception ex)
            ...{
                Console.WriteLine(ex.Message);
            }

        }


        /**//// <summary>
        /// 打印數據
        /// </summary>
        public void Print()
        ...{
            printDocument.PrintPage+=new PrintPageEventHandler(printDocument_PrintPage);
            try
            ...{
                pageSetupDialog=new PageSetupDialog();
               


; pageSetupDialog.Document=printDocument;
                pageSetupDialog.Document.DefaultPageSettings.Landscape=true;
                if(pageSetupDialog.ShowDialog()==DialogResult.OK)
                ...{
                    printPreviewDialog=new PrintPrevIEwDialog();
                    printPrevIEwDialog.Document=printDocument;
                    printPrevIEwDialog.Height=600;
                    printPrevIEwDialog.Width=800;
                    printPrevIEwDialog.ShowDialog();
                }
            }
            catch(Exception e)
            ...{    
                throw new Exception("打印出現錯誤:"+e.Message);
            }
        }
    }
}

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved