求winform下C#实现打印的代码?
求winform下C#实现打印的代码?
------解决方案--------------------MSDN上看:PrintDocument
------解决方案--------------------C# 打印DataGrid内容的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing.Printing;
namespace SimpleEditor
{
public partial class PrintGridView : Form
{
string sSqlConnection = @"Data Source=MYPC\SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa123";
//打印文档
PrintDocument pdDocument = new PrintDocument();
//打印格式设置页面
PageSetupDialog dlgPageSetup = new PageSetupDialog();
//打印页面
PrintDialog dlgPrint = new PrintDialog();
//实例化打印预览
PrintPreviewDialog dlgPrintPreview = new PrintPreviewDialog();
public PrintGridView()
{
InitializeComponent();
btnPrint.Click += new EventHandler(btnPrint_Click);
btnPrintView.Click += new EventHandler(btnPrintView_Click);
pdDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
//页面设置的打印文档设置为需要打印的文档
dlgPageSetup.Document = pdDocument;
//打印界面的打印文档设置为被打印文档
dlgPrint.Document = pdDocument;
//打印预览的打印文档设置为被打印文档
dlgPrintPreview.Document = pdDocument;
GetData();
}
///
/// 打印预览
///
///
///
void btnPrintView_Click(object sender, EventArgs e)
{
//显示打印预览界面
dlgPrintPreview.ShowDialog();
}