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. The source code is as follows:
Using system.collections; using system.componentmodel; using system.windows.form; using system.data; using system.data.oledb;
namespace DataGridPrint {///
private System.Data.SqlClient.SqlCommand sqlDeleteCommand1; private System.Data.SqlClient.SqlConnection sqlConnection1; ///
// // Todo: add any constructor code after initializationcomponent call //}
///
#region Windows Form Designer generated code ///
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 (); ("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;
this.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.Width = 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.dataGridTextBoxColumn8.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 = "Pho ne "; 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.miprintp review.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; this.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, Count ry, 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 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_REGON IS NULL AND Region Is Null); Select Customerid, CompanyName, ContactName, ContactTitle, Address, City, Region, Postalcode, Country, Phone, Fax from Customers Where (Customerid = @custom erID) "; 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 System.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.sqlupdateco mmand1.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) ((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) (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) ((System.byte) (0)), "ContactName", System.Data.DataRowVersio n.Original, null)); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_ContactTitle", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((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.datarowversion.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) (0 )), (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) ((System.byte) (0)), "Postalcode", System.Data.DataRowVersion.original, NULL); this.sqlupdateCommand1.Parameters.Add (New System) .Data.sqlclient.sqlparameter ("@ original_region", system.data.sqldbtype.nvarchar, 15, system.data.parameterDirection.input, Fals e, ((System.byte) (0)), ((System.byte) (0)), "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 (contacttitle = @original_contacttitle @
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) (0)), (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.Input, false, (( System.byte) (0)), ((System.byte) (0)), "CompanyName"
, System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection ((System.byte) ((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, ((System.byte) ((System.byte) (0)), "Country", System.Data.DATAROWVERSION.ORIGINAL, NULL ))); This.sqldeleteCommand1.parameters.add (new system.data.sqlclient.sqlparameter ("@ Original_fax", System.d Ata.sqldbtype.nvarchar, 24, System.Data.ParameterDirection.Input, False, ((System.byte) ((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) (0)), "" "" .Sqlparameter ("@ Original_Postalcode"
System.data.sqldbtype.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)), "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
///
private void btnFillDataGrid_Click (object sender, System.EventArgs e) {DataTable dt = new DataTable ( "Customers"); try {sqlDataAdapter1.Fill (dt); dgTest.DataSource = dt;} catch (System.Exception ex) {MessageBox. Show (this, ex. amount);}}
private void miPrintPreview_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;
DialogResult dlgresult = printdlg.showdialog (this); if (dlgResult == DialogResult.ok) dpd.print ();}
Private void MIEXIT_CLICK (Object Sender, System.EventArgs E) {Application.exit ();}}}