日期:2013-08-30  浏览次数:20385 次

eg:
private DataGridPrinter m_oDataGridPrinter;
protected System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Windows.Forms.PrintDialog printDialog1;
public System.Drawing.Printing.PrintDocument printDocument1;
// 预览
try
            {   
m_oDataGridPrinter = new DataGridPrinter(this.dataGrid_Brand, printDocument1, (DataTable)this.dataGrid_Brand.DataSource);
                this.printPreviewDialog1.ShowDialog();
catch
            {
                MessageBox.Show("没有找到打印机,不能预览!");
            }


// 打印
        public DialogResult Print()
        {
            try
            {
//                DataTable oDTTmp = (DataTable)this.dataGrid_Brand.DataSource;
//                if (oDTTmp==null || oDTTmp.Rows.Count<=0)
//                {
//                    MessageBox.Show("主窗口中没有数据,请进行\"查询\"或者\"显示所有\"等操作,将您要打印的数据显示在主窗口中,然后再进行打印!");
//                    return DialogResult.Cancel;                   
//                }
                m_oDataGridPrinter = new DataGridPrinter(this.dataGrid_Brand, printDocument1, (DataTable)this.dataGrid_Brand.DataSource);
               
                if (this.printDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.printDocument1.Print();
                    return DialogResult.OK;
                }
                else
                {
                    return DialogResult.Cancel;
                }
            }
            catch
            {
                MessageBox.Show("没有找到打印机,不能打印!");
                return DialogResult.Cancel;
            }
            return DialogResult.OK;
        }

// 事件定义
this.printDocument1.DocumentName = "Brand";
            this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.prin