扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
很多人都在论坛里问,如何实现DATAGRID的打印预览和打印,现在我就把这方面的源代码告诉大家。这段代码也花费了我1个晚上的时间,呵呵!数据库是基于sql server2000自带的northwind。源代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace DataGridPrint
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.DataGrid dgTest;
private System.Windows.Forms.Button btnFillDataGrid;
private System.Windows.Forms.MainMenu mnuFile;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem miPrintPreview;
private System.Windows.Forms.MenuItem miPrint;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem miExit;
private System.Windows.Forms.DataGridTableStyle dgtsCustomers;
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 dataGridTextBoxColumn6;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn7;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn8;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn9;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn10;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn11;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn5;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.dgTest = new System.Windows.Forms.DataGrid();
this.dgtsCustomers = 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.dataGridTextBoxColumn10 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn11 = new System.Windows.Forms.DataGridTextBoxColumn();
this.btnFillDataGrid = new System.Windows.Forms.Button();
this.mnuFile = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.miPrintPreview = new System.Windows.Forms.MenuItem();
this.miPrint = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.miExit = new System.Windows.Forms.MenuItem();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dgTest)).BeginInit();
this.SuspendLayout();
//
// panel1
//
this.panel1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.dgTest});
this.panel1.Location = new System.Drawing.Point(0, 9);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(788, 398);
this.panel1.TabIndex = 1;
//
// dgTest
//
this.dgTest.DataMember = "";
this.dgTest.Dock = System.Windows.Forms.DockStyle.Fill;
this.dgTest.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgTest.Name = "dgTest";
this.dgTest.Size = new System.Drawing.Size(788, 398);
this.dgTest.TabIndex = 0;
this.dgTest.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
this.dgtsCustomers});
//
// dgtsCustomers
//
this.dgtsCustomers.DataGrid = this.dgTest;
婵犵數濮烽。浠嬪焵椤掆偓閸熷潡鍩€椤掆偓缂嶅﹪骞冨Ο璇茬窞闁归偊鍓涢悾娲⒑闂堟单鍫ュ疾濠婂嫭鍙忔繝濠傜墛閸嬨劍銇勯弽銊с€掗柟钘夊暣閺岀喖鎮滈埡鍌涚彋閻庤娲樺畝绋跨暦閸洖鐓涢柛灞剧矋濞堟悂姊绘担绛嬪殐闁搞劋鍗冲畷銏ゅ冀椤愩儱小闂佹寧绋戠€氼參宕伴崱妯镐簻闁靛牆鎳庢慨顒€鈹戦埥鍡椾簼婵犮垺锚铻炴俊銈呮噺閸嬪倹绻涢崱妯诲碍閻庢艾顦甸弻宥堫檨闁告挾鍠庨锝夘敆娓氬﹦鐭楁繛鎾村焹閸嬫捇鏌e☉娆愬磳闁哄本绋戦埞鎴﹀川椤曞懏鈻婄紓鍌欑劍椤ㄥ懘鎯岄崒鐐靛祦閹兼番鍔岄悞鍨亜閹烘垵顏╅悗姘槹閵囧嫰寮介妸褎鍣ョ紓浣筋嚙濡繈寮婚悢纰辨晣鐟滃秹鎮橀懠顒傜<閺夊牄鍔庣粻鐐烘煛鐏炶姤鍠橀柡浣瑰姍瀹曠喖顢橀悩铏钒闂備浇宕垫慨鎶芥⒔瀹ュ鍨傞柦妯猴級閿濆绀嬫い鏍ㄧ☉濞堟粓姊虹涵鍛【妞ゎ偅娲熼崺鈧い鎺嗗亾闁挎洩濡囧Σ鎰板籍閸繄顓洪梺缁樺姇瀵剙螖閸涱喚鍘搁梺鍓插亽閸嬪嫰鎮橀敃鍌涚厱閻庯綆鍋嗘晶顒傜磼閸屾稑绗ч柟鐟板閹煎湱鎲撮崟闈涙櫏闂傚倷绀侀幖顐も偓姘卞厴瀹曞綊鏌嗗鍛紱閻庡箍鍎遍ˇ浼村磿瀹ュ鐓曢柡鍥ュ妼婢ь垰霉閻樿秮顏堟箒闂佹寧绻傚Λ妤呭煝閺囥垺鐓冪憸婊堝礈濮樿泛钃熼柕濞у嫷鍋ㄩ梺缁樺姇椤曨參鍩㈤弴銏″€甸柨婵嗗€瑰▍鍥ㄣ亜韫囨稐鎲鹃柡灞炬礋瀹曢亶顢橀悢濂変紦