|
很多人都在论坛里问,如何实现DATAGRID的打印预览和打印,现在我就把这方面的源代码告诉大家。这段代码也花费了我1个晚上的时间,呵呵!数据库是基于sql server2000自带的northwind。源代码如下: NqpLinux联盟 NqpLinux联盟 using System; NqpLinux联盟 using System.Drawing; NqpLinux联盟 using System.Collections; NqpLinux联盟 using System.ComponentModel; NqpLinux联盟 using System.Windows.Forms; NqpLinux联盟 using System.Data; NqpLinux联盟 using System.Data.OleDb; NqpLinux联盟 NqpLinux联盟 namespace DataGridPrint NqpLinux联盟 { NqpLinux联盟 /// <summary> NqpLinux联盟 /// Summary description for Form1. NqpLinux联盟 /// </summary> NqpLinux联盟 public class Form1 : System.Windows.Forms.Form NqpLinux联盟 { NqpLinux联盟 private System.Windows.Forms.Panel panel1; NqpLinux联盟 private System.Windows.Forms.DataGrid dgTest; NqpLinux联盟 private System.Windows.Forms.Button btnFillDataGrid; NqpLinux联盟 private System.Windows.Forms.MainMenu mnuFile; NqpLinux联盟 private System.Windows.Forms.MenuItem menuItem1; NqpLinux联盟 private System.Windows.Forms.MenuItem miPrintPreview; NqpLinux联盟 private System.Windows.Forms.MenuItem miPrint; NqpLinux联盟 private System.Windows.Forms.MenuItem menuItem4; NqpLinux联盟 private System.Windows.Forms.MenuItem miExit; NqpLinux联盟 private System.Windows.Forms.DataGridTableStyle dgtsCustomers; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn3; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn4; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn6; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn7; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn8; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn9; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn10; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn11; NqpLinux联盟 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn5; NqpLinux联盟 private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; NqpLinux联盟 private System.Data.SqlClient.SqlCommand sqlSelectCommand1; NqpLinux联盟 private System.Data.SqlClient.SqlCommand sqlInsertCommand1; NqpLinux联盟 private System.Data.SqlClient.SqlCommand sqlUpdateCommand1; NqpLinux联盟 private System.Data.SqlClient.SqlCommand sqlDeleteCommand1; NqpLinux联盟 private System.Data.SqlClient.SqlConnection sqlConnection1; NqpLinux联盟 /// <summary> NqpLinux联盟 /// Required designer variable. NqpLinux联盟 /// </summary> NqpLinux联盟 private System.ComponentModel.Container components = null; NqpLinux联盟 NqpLinux联盟 public Form1() NqpLinux联盟 { NqpLinux联盟 // NqpLinux联盟 // Required for Windows Form Designer support NqpLinux联盟 // NqpLinux联盟 InitializeComponent(); NqpLinux联盟 NqpLinux联盟 // NqpLinux联盟 // TODO: Add any constructor code after InitializeComponent call NqpLinux联盟 // NqpLinux联盟 } NqpLinux联盟 NqpLinux联盟 /// <summary> NqpLinux联盟 /// Clean up any resources being used. NqpLinux联盟 /// </summary> NqpLinux联盟 protected override void Dispose( bool disposing ) NqpLinux联盟 { NqpLinux联盟 if( disposing ) NqpLinux联盟 { NqpLinux联盟 if (components != null) NqpLinux联盟 { NqpLinux联盟 components.Dispose(); NqpLinux联盟 } NqpLinux联盟 } NqpLinux联盟 base.Dispose( disposing ); NqpLinux联盟 } NqpLinux联盟 NqpLinux联盟 #region Windows Form Designer generated code NqpLinux联盟 /// <summary> NqpLinux联盟 /// Required method for Designer support - do not modify NqpLinux联盟 /// the contents of this method with the code editor. NqpLinux联盟 /// </summary> NqpLinux联盟 private void InitializeComponent() NqpLinux联盟 { NqpLinux联盟 this.panel1 = new System.Windows.Forms.Panel(); NqpLinux联盟 this.dgTest = new System.Windows.Forms.DataGrid(); NqpLinux联盟 this.dgtsCustomers = new System.Windows.Forms.DataGridTableStyle(); NqpLinux联盟 this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.dataGridTextBoxColumn3 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.dataGridTextBoxColumn4 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.dataGridTextBoxColumn5 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.dataGridTextBoxColumn6 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.dataGridTextBoxColumn7 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.dataGridTextBoxColumn8 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.dataGridTextBoxColumn9 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.dataGridTextBoxColumn10 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.dataGridTextBoxColumn11 = new System.Windows.Forms.DataGridTextBoxColumn(); NqpLinux联盟 this.btnFillDataGrid = new System.Windows.Forms.Button(); NqpLinux联盟 this.mnuFile = new System.Windows.Forms.MainMenu(); NqpLinux联盟 this.menuItem1 = new System.Windows.Forms.MenuItem(); NqpLinux联盟 this.miPrintPreview = new System.Windows.Forms.MenuItem(); NqpLinux联盟 this.miPrint = new System.Windows.Forms.MenuItem(); NqpLinux联盟 this.menuItem4 = new System.Windows.Forms.MenuItem(); NqpLinux联盟 this.miExit = new System.Windows.Forms.MenuItem(); NqpLinux联盟 this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(); NqpLinux联盟 this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); NqpLinux联盟 this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand(); NqpLinux联盟 this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand(); NqpLinux联盟 this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand(); NqpLinux联盟 this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); NqpLinux联盟 this.panel1.SuspendLayout(); NqpLinux联盟 ((System.ComponentModel.ISupportInitialize)(this.dgTest)).BeginInit(); NqpLinux联盟 this.SuspendLayout(); NqpLinux联盟 // NqpLinux联盟 // panel1 NqpLinux联盟 // NqpLinux联盟 this.panel1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) NqpLinux联盟 | System.Windows.Forms.AnchorStyles.Left) NqpLinux联盟 | System.Windows.Forms.AnchorStyles.Right); NqpLinux联盟 this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] { NqpLinux联盟 this.dgTest}); NqpLinux联盟 this.panel1.Location = new System.Drawing.Point(0, 9); NqpLinux联盟 this.panel1.Name = "panel1"; NqpLinux联盟 this.panel1.Size = new System.Drawing.Size(788, 398); NqpLinux联盟 this.panel1.TabIndex = 1; NqpLinux联盟 // NqpLinux联盟 // dgTest NqpLinux联盟 // NqpLinux联盟 this.dgTest.DataMember = ""; NqpLinux联盟 this.dgTest.Dock = System.Windows.Forms.DockStyle.Fill; NqpLinux联盟 this.dgTest.HeaderForeColor = System.Drawing.SystemColors.ControlText; NqpLinux联盟 this.dgTest.Name = "dgTest"; NqpLinux联盟 this.dgTest.Size = new System.Drawing.Size(788, 398); NqpLinux联盟 this.dgTest.TabIndex = 0; NqpLinux联盟 this.dgTest.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] { NqpLinux联盟 this.dgtsCustomers}); NqpLinux联盟 // NqpLinux联盟 // dgtsCustomers NqpLinux联盟 // NqpLinux联盟 this.dgtsCustomers.DataGrid = this.dgTest; NqpLinux联盟 this.dgtsCustomers.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] { NqpLinux联盟 this.dataGridTextBoxColumn1, NqpLinux联盟 this.dataGridTextBoxColumn2, NqpLinux联盟 this.dataGridTextBoxColumn3, NqpLinux联盟 this.dataGridTextBoxColumn4, NqpLinux联盟 this.dataGridTextBoxColumn5, NqpLinux联盟 this.dataGridTextBoxColumn6, NqpLinux联盟 this.dataGridTextBoxColumn7, NqpLinux联盟 this.dataGridTextBoxColumn8, NqpLinux联盟 this.dataGridTextBoxColumn9, NqpLinux联盟 this.dataGridTextBoxColumn10, NqpLinux联盟 this.dataGridTextBoxColumn11}); NqpLinux联盟 this.dgtsCustomers.HeaderForeColor = System.Drawing.SystemColors.ControlText; NqpLinux联盟 this.dgtsCustomers.MappingName = "Customers"; NqpLinux联盟 this.dgtsCustomers.ReadOnly = true; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn1 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn1.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn1.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn1.HeaderText = "Customer Id"; NqpLinux联盟 this.dataGridTextBoxColumn1.MappingName = "CustomerId"; NqpLinux联盟 this.dataGridTextBoxColumn1.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn1.Width = 75; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn2 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn2.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn2.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn2.HeaderText = "Company Name"; NqpLinux联盟 this.dataGridTextBoxColumn2.MappingName = "CompanyName"; NqpLinux联盟 this.dataGridTextBoxColumn2.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn2.Width = 200; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn3 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn3.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn3.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn3.HeaderText = "Contact Name"; NqpLinux联盟 this.dataGridTextBoxColumn3.MappingName = "ContactName"; NqpLinux联盟 this.dataGridTextBoxColumn3.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn3.Width = 125; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn4 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn4.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn4.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn4.HeaderText = "Contact Title"; NqpLinux联盟 this.dataGridTextBoxColumn4.MappingName = "ContactTitle"; NqpLinux联盟 this.dataGridTextBoxColumn4.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn4.Width = 170; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn5 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn5.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn5.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn5.HeaderText = "Address"; NqpLinux联盟 this.dataGridTextBoxColumn5.MappingName = "Address"; NqpLinux联盟 this.dataGridTextBoxColumn5.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn5.Width = 250; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn6 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn6.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn6.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn6.HeaderText = "City"; NqpLinux联盟 this.dataGridTextBoxColumn6.MappingName = "City"; NqpLinux联盟 this.dataGridTextBoxColumn6.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn6.Width = 150; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn7 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn7.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn7.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn7.HeaderText = "Region"; NqpLinux联盟 this.dataGridTextBoxColumn7.MappingName = "Region"; NqpLinux联盟 this.dataGridTextBoxColumn7.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn7.Width = 150; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn8 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn8.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn8.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn8.HeaderText = "Postal Code"; NqpLinux联盟 this.dataGridTextBoxColumn8.MappingName = "PostalCode"; NqpLinux联盟 this.dataGridTextBoxColumn8.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn8.Width = 150; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn9 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn9.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn9.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn9.HeaderText = "Country"; NqpLinux联盟 this.dataGridTextBoxColumn9.MappingName = "Country"; NqpLinux联盟 this.dataGridTextBoxColumn9.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn9.Width = 125; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn10 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn10.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn10.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn10.HeaderText = "Phone"; NqpLinux联盟 this.dataGridTextBoxColumn10.MappingName = "Phone"; NqpLinux联盟 this.dataGridTextBoxColumn10.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn10.Width = 125; NqpLinux联盟 // NqpLinux联盟 // dataGridTextBoxColumn11 NqpLinux联盟 // NqpLinux联盟 this.dataGridTextBoxColumn11.Format = ""; NqpLinux联盟 this.dataGridTextBoxColumn11.FormatInfo = null; NqpLinux联盟 this.dataGridTextBoxColumn11.HeaderText = "Fax"; NqpLinux联盟 this.dataGridTextBoxColumn11.MappingName = "Fax"; NqpLinux联盟 this.dataGridTextBoxColumn11.NullText = ""; NqpLinux联盟 this.dataGridTextBoxColumn11.Width = 125; NqpLinux联盟 // NqpLinux联盟 // btnFillDataGrid NqpLinux联盟 // NqpLinux联盟 this.btnFillDataGrid.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right); NqpLinux联盟 this.btnFillDataGrid.Location = new System.Drawing.Point(645, 426); NqpLinux联盟 this.btnFillDataGrid.Name = "btnFillDataGrid"; NqpLinux联盟 this.btnFillDataGrid.Size = new System.Drawing.Size(133, 28); NqpLinux联盟 this.btnFillDataGrid.TabIndex = 2; NqpLinux联盟 this.btnFillDataGrid.Text = "&Fill DataGrid"; NqpLinux联盟 this.btnFillDataGrid.Click += new System.EventHandler(this.btnFillDataGrid_Click); NqpLinux联盟 // NqpLinux联盟 // mnuFile NqpLinux联盟 // NqpLinux联盟 this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { NqpLinux联盟 this.menuItem1}); NqpLinux联盟 // NqpLinux联盟 // menuItem1 NqpLinux联盟 // NqpLinux联盟 this.menuItem1.Index = 0; NqpLinux联盟 this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { NqpLinux联盟 this.miPrintPreview, NqpLinux联盟 this.miPrint, NqpLinux联盟 this.menuItem4, NqpLinux联盟 this.miExit}); NqpLinux联盟 this.menuItem1.Text = "&File"; NqpLinux联盟 // NqpLinux联盟 // miPrintPreview NqpLinux联盟 // NqpLinux联盟 this.miPrintPreview.Index = 0; NqpLinux联盟 this.miPrintPreview.Text = "Print Pre&view"; NqpLinux联盟 this.miPrintPreview.Click += new System.EventHandler(this.miPrintPreview_Click); NqpLinux联盟 // NqpLinux联盟 // miPrint NqpLinux联盟 // NqpLinux联盟 this.miPrint.Index = 1; NqpLinux联盟 this.miPrint.Text = "&Print"; NqpLinux联盟 this.miPrint.Click += new System.EventHandler(this.miPrint_Click); NqpLinux联盟 // NqpLinux联盟 // menuItem4 NqpLinux联盟 // NqpLinux联盟 this.menuItem4.Index = 2; NqpLinux联盟 this.menuItem4.Text = "-"; NqpLinux联盟 // NqpLinux联盟 // miExit NqpLinux联盟 // NqpLinux联盟 this.miExit.Index = 3; NqpLinux联盟 this.miExit.Text = "E&xit"; NqpLinux联盟 this.miExit.Click += new System.EventHandler(this.miExit_Click); NqpLinux联盟 // NqpLinux联盟 // sqlDataAdapter1 NqpLinux联盟 // NqpLinux联盟 this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1; NqpLinux联盟 this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1; NqpLinux联盟 this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1; NqpLinux联盟 this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { NqpLinux联盟 new System.Data.Common.DataTableMapping("Table", "Customers", new System.Data.Common.DataColumnMapping[] { NqpLinux联盟 new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"), NqpLinux联盟 new System.Data.Common.DataColumnMapping("CompanyName", "CompanyName"), NqpLinux联盟 new System.Data.Common.DataColumnMapping("ContactName", "ContactName"), NqpLinux联盟 new System.Data.Common.DataColumnMapping("ContactTitle", "ContactTitle"), NqpLinux联盟 new System.Data.Common.DataColumnMapping("Address", "Address"), NqpLinux联盟 new System.Data.Common.DataColumnMapping("City", "City"), NqpLinux联盟 new System.Data.Common.DataColumnMapping("Region", "Region"), NqpLinux联盟 new System.Data.Common.DataColumnMapping("PostalCode", "PostalCode"), NqpLinux联盟 new System.Data.Common.DataColumnMapping("Country", "Country"), NqpLinux联盟 new System.Data.Common.DataColumnMapping("Phone", "Phone"), NqpLinux联盟 new System.Data.Common.DataColumnMapping("Fax", "Fax")})}); NqpLinux联盟 this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1; NqpLinux联盟 // NqpLinux联盟 // sqlSelectCommand1 NqpLinux联盟 // NqpLinux联盟 this.sqlSelectCommand1.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region," + NqpLinux联盟 " PostalCode, Country, Phone, Fax FROM Customers"; NqpLinux联盟 this.sqlSelectCommand1.Connection = this.sqlConnection1; NqpLinux联盟 // NqpLinux联盟 // sqlInsertCommand1 NqpLinux联盟 // NqpLinux联盟 this.sqlInsertCommand1.CommandText = @"INSERT INTO Customers(CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)"; NqpLinux联盟 this.sqlInsertCommand1.Connection = this.sqlConnection1; NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID")); NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName")); NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactName", System.Data.SqlDbType.NVarChar, 30, "ContactName")); NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle")); NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address")); NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City")); NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region")); NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode")); NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.NVarChar, 15, "Country")); NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.NVarChar, 24, "Phone")); NqpLinux联盟 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.NVarChar, 24, "Fax")); NqpLinux联盟 // NqpLinux联盟 // sqlUpdateCommand1 NqpLinux联盟 // NqpLinux联盟 this.sqlUpdateCommand1.CommandText = @"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName, ContactName = @ContactName, ContactTitle = @ContactTitle, Address = @Address, City = @City, Region = @Region, PostalCode = @PostalCode, Country = @Country, Phone = @Phone, Fax = @Fax WHERE (CustomerID = @Original_CustomerID) AND (Address = @Original_Address OR @Original_Address IS NULL AND Address IS NULL) AND (City = @Original_City OR @Original_City IS NULL AND City IS NULL) AND (CompanyName = @Original_CompanyName) AND (ContactName = @Original_ContactName OR @Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @Original_ContactTitle OR @Original_ContactTitle IS NULL AND ContactTitle IS NULL) AND (Country = @Original_Country OR @Original_Country IS NULL AND Country IS NULL) AND (Fax = @Original_Fax OR @Original_Fax IS NULL AND Fax IS NULL) AND (Phone = @Original_Phone OR @Original_Phone IS NULL AND Phone IS NULL) AND (PostalCode = @Original_PostalCode OR @Original_PostalCode IS NULL AND PostalCode IS NULL) AND (Region = @Original_Region OR @Original_Region IS NULL AND Region IS NULL); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)"; NqpLinux联盟 this.sqlUpdateCommand1.Connection = this.sqlConnection1; NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactName", System.Data.SqlDbType.NVarChar, 30, "ContactName")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.NVarChar, 15, "Country")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.NVarChar, 24, "Phone")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.NVarChar, 24, "Fax")); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Address", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_City", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CompanyName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CompanyName", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactTitle", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactTitle", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Country", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Fax", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Fax", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Phone", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_PostalCode", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "PostalCode", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Region", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Region", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 // NqpLinux联盟 // sqlDeleteCommand1 NqpLinux联盟 // NqpLinux联盟 this.sqlDeleteCommand1.CommandText = @"DELETE FROM Customers WHERE (CustomerID = @Original_CustomerID) AND (Address = @Original_Address OR @Original_Address IS NULL AND Address IS NULL) AND (City = @Original_City OR @Original_City IS NULL AND City IS NULL) AND (CompanyName = @Original_CompanyName) AND (ContactName = @Original_ContactName OR @Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @Original_ContactTitle OR @Original_ContactTitle IS NULL AND ContactTitle IS NULL) AND (Country = @Original_Country OR @Original_Country IS NULL AND Country IS NULL) AND (Fax = @Original_Fax OR @Original_Fax IS NULL AND Fax IS NULL) AND (Phone = @Original_Phone OR @Original_Phone IS NULL AND Phone IS NULL) AND (PostalCode = @Original_PostalCode OR @Original_PostalCode IS NULL AND PostalCode IS NULL) AND (Region = @Original_Region OR @Original_Region IS NULL AND Region IS NULL)"; NqpLinux联盟 this.sqlDeleteCommand1.Connection = this.sqlConnection1; NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Address", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_City", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CompanyName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CompanyName", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactTitle", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactTitle", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Country", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Fax", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Fax", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Phone", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_PostalCode", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "PostalCode", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Region", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Region", System.Data.DataRowVersion.Original, null)); NqpLinux联盟 // NqpLinux联盟 // sqlConnection1 NqpLinux联盟 // NqpLinux联盟 this.sqlConnection1.ConnectionString = "data source=QIEYJ;initial catalog=Northwind;persist security info=False;user id=s" + NqpLinux联盟 "a;workstation id=QIEYJ;packet size=4096"; NqpLinux联盟 // NqpLinux联盟 // Form1 NqpLinux联盟 // NqpLinux联盟 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); NqpLinux联盟 this.ClientSize = new System.Drawing.Size(788, 463); NqpLinux联盟 this.Controls.AddRange(new System.Windows.Forms.Control[] { NqpLinux联盟 this.btnFillDataGrid, NqpLinux联盟 this.panel1}); NqpLinux联盟 this.Menu = this.mnuFile; NqpLinux联盟 this.Name = "Form1"; NqpLinux联盟 this.Text = "DataGrid Print Sample"; NqpLinux联盟 this.panel1.ResumeLayout(false); NqpLinux联盟 ((System.ComponentModel.ISupportInitialize)(this.dgTest)).EndInit(); NqpLinux联盟 this.ResumeLayout(false); NqpLinux联盟 NqpLinux联盟 } NqpLinux联盟 #endregion NqpLinux联盟 NqpLinux联盟 /// <summary> NqpLinux联盟 /// The main entry point for the application. NqpLinux联盟 /// </summary> NqpLinux联盟 [STAThread] NqpLinux联盟 static void Main() NqpLinux联盟 { NqpLinux联盟 Application.Run(new Form1()); NqpLinux联盟 } NqpLinux联盟 NqpLinux联盟 private void btnFillDataGrid_Click(object sender, System.EventArgs e) NqpLinux联盟 { NqpLinux联盟 DataTable dt = new DataTable("Customers"); NqpLinux联盟 try NqpLinux联盟 { NqpLinux联盟 sqlDataAdapter1.Fill(dt); NqpLinux联盟 dgTest.DataSource = dt; NqpLinux联盟 } NqpLinux联盟 catch (System.Exception ex) NqpLinux联盟 { NqpLinux联盟 MessageBox.Show(this,ex.Message); NqpLinux联盟 } NqpLinux联盟 } NqpLinux联盟 NqpLinux联盟 private void miPrintPreview_Click(object sender, System.EventArgs e) NqpLinux联盟 { NqpLinux联盟 DataGridPrintDocument dpd = new DataGridPrintDocument(dgTest); NqpLinux联盟 dpd.ColumnHeaderFont=new Font("Arial",8,FontStyle.Bold); NqpLinux联盟 dpd.DefaultPageSettings.Landscape = true; NqpLinux联盟 NqpLinux联盟 PrintPreviewDialog PrintPrevDlg=new PrintPreviewDialog(); NqpLinux联盟 PrintPrevDlg.Document = dpd; NqpLinux联盟 PrintPrevDlg.ShowDialog(this); NqpLinux联盟 } NqpLinux联盟 NqpLinux联盟 private void miPrint_Click(object sender, System.EventArgs e) NqpLinux联盟 { NqpLinux联盟 NqpLinux联盟 DataGridPrintDocument dpd = new DataGridPrintDocument(dgTest); NqpLinux联盟 dpd.DefaultPageSettings.Landscape = true; NqpLinux联盟 NqpLinux联盟 PrintDialog PrintDlg=new PrintDialog(); NqpLinux联盟 PrintDlg.Document = dpd; NqpLinux联盟 NqpLinux联盟 DialogResult dlgresult=PrintDlg.ShowDialog(this); NqpLinux联盟 if (dlgresult==DialogResult.OK) NqpLinux联盟 dpd.Print(); NqpLinux联盟 NqpLinux联盟 } NqpLinux联盟 NqpLinux联盟 private void miExit_Click(object sender, System.EventArgs e) NqpLinux联盟 { NqpLinux联盟 Application.Exit(); NqpLinux联盟 } NqpLinux联盟 } NqpLinux联盟 }
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|