Print preview and printing of DataGrid in C #

xiaoxiao2021-03-06  39

Author Blog:

http://blog.9cbs.net/qieyj/

Many people ask in the forum, how to implement DataGrid print preview and printing, now I will tell you this source code. This code also spends me 1 night, huh, huh! The database is Northwind based on SQL Server2000.

Source code is as follows: 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 description for Form1 /// 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.DataGridt extBoxColumn 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; ///

/// Required designer variable /// private System.ComponentModel.Container components = null;. public Form1 () {// // Required for Windows Form Designer support // InitializeComponent () ; // // Todo: add any constructor code after initializecomponent call ///} /// /// clean up any resources being used. /// protected Override Void Dispose (Bool Disposing) {ix (Disposing) {if (component! = null) {Components.dispose ();}} Base.Dispose (4);} #region windows form designer generated code /// /// Required method for Designer Support - Do Not Modify /// THE Contents of This method with the code editor. /// 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.mainu (); 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.sq lClient.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.Windows.Windows.Windows.Forms.Anchorstyles.Bottom) | System.Windows.Forms.Anchorstyles.Left | System.Windows .Form.anchorstyles.right;

This.Panel1.controls.addRange (new system.windows.dgtest}); this.pal1.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 ;.dgtscustomers.gridcolumnstyles.addrange (new system.windows.forms.dataGridColumnstyle [] {this.DataGridtextBoxColumn1, this.DataGridTextBoxColumn2, this.dataGridTextBoxColumn3, this.dataGridTextBoxColumn4, this.dataGridTextBoxColumn5, this.dataGridTextBoxColumn6, this.dataGridTextBoxColumn7, this.dataGridTextBoxColumn8, this.dataGridTextBoxColumn9, this.dataGridTextBoxColumn10, this.dataGridTextBoxColumn11}); this.dgtsCustomers.HeaderForeColor = System.Drawing.SystemColors. ControlText; this.dgtscustomers.mappingname = "customers"; this.dgtscustomers.Readonly = true;

// // dataGridTextBoxColumn1 // this.dataGridTextBoxColumn1.Format = ""; this.dataGridTextBoxColumn1.FormatInfo = null; this.dataGridTextBoxColumn1.HeaderText = "Customer Id"; this.dataGridTextBoxColumn1.MappingName = "CustomerId"; this.dataGridTextBoxColumn1.NullText = ""; this.dataGridTextBoxColumn1.Width = 75; // // dataGridTextBoxColumn2 // this.dataGridTextBoxColumn2.Format = ""; this.dataGridTextBoxColumn2.FormatInfo = null; this.dataGridTextBoxColumn2.HeaderText = "Company Name"; this.dataGridTextBoxColumn2 .MappingName = "CompanyName"; this.dataGridTextBoxColumn2.NullText = ""; this.dataGridTextBoxColumn2.Width = 200; // // dataGridTextBoxColumn3 // this.dataGridTextBoxColumn3.Format = ""; this.dataGridTextBoxColumn3.FormatInfo = null; this. dataGridTextBoxColumn3.HeaderText = "Contact Name"; this.dataGridTextBoxColumn3.MappingName = "ContactName"; this.dataGridTextBoxColumn3.NullText = ""; this.dataGridTextBoxColumn3.Widt h 125 =; // // dataGridTextBoxColumn4 // this.dataGridTextBoxColumn4.Format = ""; this.dataGridTextBoxColumn4.FormatInfo = null; this.dataGridTextBoxColumn4.HeaderText = "Contact Title"; this.dataGridTextBoxColumn4.MappingName = "ContactTitle"; this .dataGridTextBoxColumn4.NullText = ""; this.dataGridTextBoxColumn4.Width = 170; // // dataGridTextBoxColumn5 // this.dataGridTextBoxColumn5.Format = ""; this.dataGridTextBoxColumn5.FormatInfo = null;

this.dataGridTextBoxColumn5.HeaderText = "Address"; this.dataGridTextBoxColumn5.MappingName = "Address"; this.dataGridTextBoxColumn5.NullText = ""; this.dataGridTextBoxColumn5.Width = 250; // // dataGridTextBoxColumn6 // this.dataGridTextBoxColumn6.Format = ""; this.dataGridTextBoxColumn6.FormatInfo = null; this.dataGridTextBoxColumn6.HeaderText = "City"; this.dataGridTextBoxColumn6.MappingName = "City"; this.dataGridTextBoxColumn6.NullText = ""; this.dataGridTextBoxColumn6.Width = 150; // // dataGridTextBoxColumn7 // this.dataGridTextBoxColumn7.Format = ""; this.dataGridTextBoxColumn7.FormatInfo = null; this.dataGridTextBoxColumn7.HeaderText = "Region"; this.dataGridTextBoxColumn7.MappingName = "Region"; this.dataGridTextBoxColumn7.NullText = "" ; this.dataGridTextBoxColumn7.Width = 150; // // dataGridTextBoxColumn8 // this.dataGridTextBoxColumn8.Format = ""; this.dataGridTextBoxColumn8.FormatInfo = null; this.dataGridTextBoxC olumn8.HeaderText = "Postal Code"; this.dataGridTextBoxColumn8.MappingName = "PostalCode"; this.dataGridTextBoxColumn8.NullText = ""; this.dataGridTextBoxColumn8.Width = 150; // // dataGridTextBoxColumn9 // this.dataGridTextBoxColumn9.Format = " "; this.dataGridTextBoxColumn9.FormatInfo = null; this.dataGridTextBoxColumn9.HeaderText =" Country "; this.dataGridTextBoxColumn9.MappingName =" Country "; this.dataGridTextBoxColumn9.NullText =" ";

this.dataGridTextBoxColumn9.Width = 125; // // dataGridTextBoxColumn10 // this.dataGridTextBoxColumn10.Format = ""; this.dataGridTextBoxColumn10.FormatInfo = null; this.dataGridTextBoxColumn10.HeaderText = "Phone"; this.dataGridTextBoxColumn10.MappingName = "Phone "; this.dataGridTextBoxColumn10.NullText =" "; this.dataGridTextBoxColumn10.Width = 125; // // dataGridTextBoxColumn11 // this.dataGridTextBoxColumn11.Format =" "; this.dataGridTextBoxColumn11.FormatInfo = null; this.dataGridTextBoxColumn11.HeaderText =" Fax "; this.dataGridTextBoxColumn11.MappingName =" Fax "; this.dataGridTextBoxColumn11.NullText =" "; this.dataGridTextBoxColumn11.Width = 125; // // btnFillDataGrid // this.btnFillDataGrid.Anchor = (System.Windows.Forms. AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right); this.btnFillDataGrid.Location = new System.Drawing.Point (645, 426); this.btnFillDataGrid.Name = "btnFillDataGrid"; this.btnFillDataGrid.Size = new System.Drawing.Size (133, 28); this.btnFillDataGrid.TabIndex = 2; this.btnFillDataGrid.Text = "& Fill DataGrid"; this.btnFillDataGrid.Click = new System.EventHandler (this.btnFillDataGrid_Click); // // mnufile // this.mnufile.Menuitems.addrange (new system.windows.forms.Menuitem [] {this.Menuitem1}); /// Menuitem1 // this.Menuitem1.index = 0;

This.Menuitem1.Menuitems.addrange (New System.windows.Forms.Menuitem [] {this.miprintpreview, this.miprint, this.MenuItem4, this.miexit}; this.Menuitem1.text = "& file"; // / / miPrintPreview // this.miPrintPreview.Index = 0; this.miPrintPreview.Text = "Print Pre & view"; this.miPrintPreview.Click = new System.EventHandler (this.miPrintPreview_Click); // // miPrint // this.miPrint .Index = 1; this.Miprint.text = "& print"; this.miprint.click = new system.eventhandler (this.Miprint_click); // // Menuitem4 // this.Menuitem4.index = 2; this.Menuitem4 .Text = "-"; // // MIEXIT // this.miexit.index = 3; this.miexit.text = "e & xit"; this.miexit.click = new system.eventhandler (this.miexit_click); / / // sqldataadapter1 // this.sqldataadapter1.deleteCommand = this.sqlDeleteCommand1; this.sqldataadapter1.insertcommand = this.sqlinsertcommand1; thi s.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1; this.sqlDataAdapter1.TableMappings.AddRange (new System.Data.Common.DataTableMapping [] {new System.Data.Common.DataTableMapping ( "Table", "Customers", new System.Data .Common.datacolumnmapping [] {new system.data.common.datacolumnmapping ("Customerid", "Customerid"), new system.data.common.DatacolumnMapping ("CompanyName", "CompanyName"

), New System.Data.Common.DataColumnMapping ( "ContactName", "ContactName"), new System.Data.Common.DataColumnMapping ( "ContactTitle", "ContactTitle"), new System.Data.Common.DataColumnMapping ( "Address" , "Address"), new system.data.common.datacolumnmapping ("city", "city"), new system.data.common.datacolumnmapping ("region", "region"), new system.data.common.Datacolumnmapping ("Postalcode", "postalcode"), new system.data.common.datacolumnmapping ("country", "country"), new system.data.common.Datacolumnmapping ("Phone", "Phone"), new System.Data.Common.DataColumnMapping ( "Fax", "Fax")})}); this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1; // // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT CustomerID, CompanyName , ContactName, ContactTitle, Address, City, Region, " " PostalCode, Country, Phone, Fax FROM Customers "; this.sqlSelectCommand1.Connection = this.sqlConnection1; // // sqlInsertCommand1 // 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) "; this.sqlInsertCommand1.Connection = this.sqlConnection1; this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID")); this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName")); this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ ContactName ", system.data.sqldbtype.nvarchar, 30," contactname ")); this.sqlinsertcommand1.parameters.add (new system.data.sqlclient.sqlparameter ( "@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle")); this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Address", System.Data.SqlDbType. NVARCHAR, 60, "Address"); this.sqlinsertcommand1.parameters.add (new system.data.sqlclient.sqlparameter ("@ City", system.data.sqldbtype.nvarchar, 15, "city"); this. SqlinsertCommand1.Parameters.add (new system.data.sqlclient.sqlparameter ("@ region", system.data.sqldbtype.nvarchar, 15, "region"));

this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode")); this.sqlInsertCommand1.Parameters.Add (new System.Data .SqlClient.SqlParameter ( "@ Country", System.Data.SqlDbType.NVarChar, 15, "Country")); this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Phone", System. Data.sqldbtype.nvarchar, 24, "phone"); this.sqlinsertcommand1.parameters.add (new system.data.sqlclient.sqlparameter ("@ fax", system.data.sqldbtype.nvarchar, 24, "fax") ); // // sqlUpdateCommand1 // 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 Addres s 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) "; this.sqlUpdateCommand1.Connection = this.sqlConnection1; this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID")); this.sqlUpdateCommand1.Parameters.Add (new System.Data .SqlClient.SqlParameter ( "@ CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName")); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ ContactName", System. Data.sqldbtype.nvarchar, 30, "contactname"); this.sqlupdateCommand1.Parameters.Add (New Syst em.Data.SqlClient.SqlParameter ( "@ ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle")); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Address" System.data.sqldbtype.nvarchar, 60, "address"); this.sqlupdateCommand1.Parameters.add (new system.data.sqlclient.sqlparameter ("@ city", system.data.sqldbtype.nvarchar, 15, " City ")); this.sqlupdateCommand1.Parameters.add (new system.data.sqlclient.sqlparameter (" @ region ", system.data.sqldbtype.nvarchar, 15," region "));

this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode")); this.sqlUpdateCommand1.Parameters.Add (new System.Data .SqlClient.SqlParameter ( "@ Country", System.Data.SqlDbType.NVarChar, 15, "Country")); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Phone", System. Data.sqldbtype.nvarchar, 24, "phone"); this.sqlupdateCommand1.Parameters.add (new system.data.sqlclient.sqlparameter ("@ fax", system.data.sqldbtype.nvarchar, 24, "fax") ); 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); this.sqlupdateCommand1.Parameters.add (new system.data.sqlclient.sqlparameter) @ORIGINAL_ADDRESS ", System.Data.sqldbtype.nvarchar, 60, System.Data.ParameterDirection .Input, false ((System.byte) ((System.byte) (0)), "Address", System.Data.DataRowVersion.original, null); this.sqlupdateCommand1.Parameters.Add (New system.data.sqlclient.sqlparameter ("@ original_city", system.data.sqldbtype.nvarchar, 15, system.data.parameterDirection.Input, false, ((System.byte) (((System.)) BYTE) (0)), "City", system.data.datarowversion.original, null); this.sqlupdateCommand1.Parameters.add (new system.data.sqlclient.sqlparameter ("@ Original_companyname"

System.data.sqldbtype.nvarchar, 40, system.data.parameterDirection.input, false, ((System.byte) ((System.byte) (0)), "CompanyName", System.data .DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false ((System.byte) (0)), "ContactName", System.Data.DataRowVersion.original, NULL); this.sqlupdateCommand1.Parameters.Add (New System. Data.sqlclient.sqlparameter ("@ Original_ContactTitTitle", System.Data.sqldbtype.nvarchar, 30, System.Data.ParameterDirection.Input, False, ((System.byte)) ((System.byte) (0) )), "ContactTitle", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_Country", System.Data.SqlDbType.NVarChar, 15 System.Data.ParameterDirection.Input, False, ((System.byte) ((System.byte) (0)), "Country", System.Data.D ataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_Fax", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.byte) ((System.byte) (0)), "fax", system.data.datarowversion.original, null); this.sqlupdateCommand1.Parameters.Add (New System.Data . Sqlclient.sqlparameter ("@ original_phone", system.data.sqldbtype.nvarchar, 24, system.data.parameterDirection.Input, false, ((System.byte)), ((System.byte) (0) ), "Phone", system.data.datarowversion.original, null);

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); this.sqlupdateCommand1.parameters.add (new system.data.sqlclient.sqlparameter) ", System.data.sqldbtype.nvarchar, 15, system.data.parameterDirection.input, false, ((System.byte) ((System.byte))," Region ", System. Data.DataRowVersion.Original, null)); // // sqlDeleteCommand1 // 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 (Cont actTitle = @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) "; this.sqlDeleteCommand1. Connection = this.sqlConnection1;

this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte) (0 )), ("Customerid", System.Data.DataRowVersion.original, null); this.sqldeleteCommand1.Parameters.add (new system.data.sqlclient.sqlparameter ("@ original_address ", System.data.sqldbtype.nvarchar, 60, system.data.parameterDirection.input, false, ((System.byte)), ((System.byte) (0))," Address ", System. Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_City", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, False, ((System.byte) ((System.byte) (0)), "City", System.Data.DataRowVersion.original, Null); this.sqldeleteCommand1.Parameters.Add (New System) .DATA.SQLCLIENT.SQLPARAMETER ("@ Original_companyName", System.Data.Sqldbtype.Nvarchar, 40, System.Data.ParameterDirection.i NPUT, FALSE, ((System.byte) ((System.byte) (0)), "CompanyName", System.Data.DataRowVersion.Riginal, Null); 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)); this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_ContactTitle", System.Data.SqlDbType. NVARCHAR, 30, System.Data.ParameterDirection.Input, false, ((System.byte) ((System.byte) (0)), "

ContactTitle ", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter (" @ Original_Country ", System.Data.SqlDbType.NVarChar, 15, System.Data .ParameterDirection.input, false (0)), ((System.byte) (0)), "Country", System.Data.DataRowVersion.original, Null); 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); this.sqldeleteCommand1.parameters.add (new system.data.sqlclient.sqlparameter ("@ Original_Phone", System.Data . Sqldbtype.nvarchar, 24, System.Data.ParameterDirection.Input, False, (System.byte) ((System.byte) (0)), "Phone", System.Data.DataRowVersion.original , null); this.sqldeleteCommand1.Parameters.add (new system.data.sqlclient.sqlparameter ("@ Original_Postalcode", System.Data.sqldbt Ype.nvarchar, 10, System.Data.ParameterDirection.Input, False, ((System.byte) ((System.byte) (0)), "Postalcode", System.Data.DataRowVersion.ORIGINAL, null)); 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); // // sqlconnection1 // this.sqlConnection1.connectionstring = "Data Source = Qieyj; Initial catalog = northwind; persist security info = false; user ID = s " " a;

Workstation id = qieyj; packet size = 4096 "; // // form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (788, 463); This.Controls.addrange (new system.windows.forms.control [] {this.btnfilldataGrid, this.Panel1}); this.Menu = this.mnufile; this.name = "form1"; this.text = "DataGrid Print Sample "; this.Panel1.ResumeLayout (FALSE); (this.dgtest)). Endinit (); this.ResumeLayout (false);} #ENDREGION ///

/// . The main entry point for the application /// [STAThread] static void main () {Application.Run (new Form1 ());} private void btnFillDataGrid_Click (object sender, System.EventArgs e) {DataTable dt = New DataTable ("CUSTOMERS"); try {sqldataadapter1.fill (dt); dgtest.dataasource = dt;} catch (system.exception ex) {messagebox.show (this, ess, ess, em);}} private void mi PrintPreview_Click (object sender, System.EventArgs e) {DataGridPrintDocument dpd = new DataGridPrintDocument (dgTest); dpd.ColumnHeaderFont = new Font ( "Arial", 8, FontStyle.Bold); dpd.DefaultPageSettings.Landscape = true; PrintPreviewDialog PrintPrevDlg = new PrintPreviewDialog (); PrintPrevDlg.Document = dpd; PrintPrevDlg.ShowDialog (this);} private void miPrint_Click (object sender, System.EventArgs e) {DataGridPrintDocument dpd = new DataGridPrintDocument (dgTest); dpd.DefaultPageSettings.Landscape = true; PrintDialog PrintDlg = New printDialog (); printdlg.document = dpd;

转载请注明原文地址:https://www.9cbs.com/read-77630.html

New Post(0)