前往顾页
以后地位: 主页 > 收集编程 > .Net实例教程 >

如安在C#里实现DATAGRID的打印预览和打印

时候:2014-12-03 20:56来源:知行网www.zhixing123.cn 编辑:麦田守望者

很多人都在论坛里问,若何实现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;
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";

顶一下
(0)
0%
踩一下
(0)
0%
------分开线----------------------------
标签(Tag):C# C#实例教程 c#根本教程 C#源代码 c#技能
------分开线----------------------------
颁发评论
请自发遵循互联网相关的政策法规,严禁公布色情、暴力、革命的谈吐。
评价:
神色:
考证码:点击我更换图片
猜你感兴趣