Click here to Skip to main content
15,880,905 members
Articles / Programming Languages / C#

DataGridView Print/Print Preview Solution - Part II

Rate me:
Please Sign up or sign in to vote.
5.00/5 (15 votes)
18 Jun 2009CPOL4 min read 77.4K   7.4K   47  
This article is the second one of two articles in which I want to show a solution for the Print Preview of the DataGridView object. One of the goals of my solution is to print the DataGridView keeping its styles automatically.
namespace GridPrintPreviewLib
{
    partial class GLPrintPreviewControl
    {
        /// <summary> 
        /// Variabile di progettazione necessaria.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Liberare le risorse in uso.
        /// </summary>
        /// <param name="disposing">ha valore true se le risorse gestite devono essere eliminate, false in caso contrario.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Codice generato da Progettazione componenti

        /// <summary> 
        /// Metodo necessario per il supporto della finestra di progettazione. Non modificare 
        /// il contenuto del metodo con l'editor di codice.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(GLPrintPreviewControl));
            this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
            this.flowLayoutPanelRight = new System.Windows.Forms.FlowLayoutPanel();
            this.lblPage = new System.Windows.Forms.Label();
            this.nudZoom = new System.Windows.Forms.NumericUpDown();
            this.lblZoom = new System.Windows.Forms.Label();
            this.pPreview = new System.Windows.Forms.Panel();
            this.pbPreviewImage = new System.Windows.Forms.PictureBox();
            this.flowLayoutPanelLeft = new System.Windows.Forms.FlowLayoutPanel();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.printToolStripMenuItem = new System.Windows.Forms.ToolStripButton();
            this.pageSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripDropDownButton();
            this.marginToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.orientationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.portraitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.landscapeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.closeToolStripMenuItem = new System.Windows.Forms.ToolStripButton();
            this.printSettingToolStripMenuItem = new System.Windows.Forms.ToolStripDropDownButton();
            this.fitSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.selectedPrinterToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
            this.flowLayoutPanelRight.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.nudZoom)).BeginInit();
            this.pPreview.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pbPreviewImage)).BeginInit();
            this.flowLayoutPanelLeft.SuspendLayout();
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // numericUpDown1
            // 
            this.numericUpDown1.Location = new System.Drawing.Point(377, 3);
            this.numericUpDown1.Name = "numericUpDown1";
            this.numericUpDown1.Size = new System.Drawing.Size(49, 20);
            this.numericUpDown1.TabIndex = 0;
            this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
            // 
            // flowLayoutPanelRight
            // 
            this.flowLayoutPanelRight.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.flowLayoutPanelRight.Controls.Add(this.numericUpDown1);
            this.flowLayoutPanelRight.Controls.Add(this.lblPage);
            this.flowLayoutPanelRight.Controls.Add(this.nudZoom);
            this.flowLayoutPanelRight.Controls.Add(this.lblZoom);
            this.flowLayoutPanelRight.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
            this.flowLayoutPanelRight.Location = new System.Drawing.Point(101, 0);
            this.flowLayoutPanelRight.Name = "flowLayoutPanelRight";
            this.flowLayoutPanelRight.Size = new System.Drawing.Size(429, 25);
            this.flowLayoutPanelRight.TabIndex = 2;
            // 
            // lblPage
            // 
            this.lblPage.Location = new System.Drawing.Point(335, 0);
            this.lblPage.Name = "lblPage";
            this.lblPage.Size = new System.Drawing.Size(36, 23);
            this.lblPage.TabIndex = 3;
            this.lblPage.Text = "Page";
            this.lblPage.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // nudZoom
            // 
            this.nudZoom.Increment = new decimal(new int[] {
            10,
            0,
            0,
            0});
            this.nudZoom.Location = new System.Drawing.Point(280, 3);
            this.nudZoom.Maximum = new decimal(new int[] {
            500,
            0,
            0,
            0});
            this.nudZoom.Minimum = new decimal(new int[] {
            10,
            0,
            0,
            0});
            this.nudZoom.Name = "nudZoom";
            this.nudZoom.Size = new System.Drawing.Size(49, 20);
            this.nudZoom.TabIndex = 1;
            this.nudZoom.Value = new decimal(new int[] {
            10,
            0,
            0,
            0});
            this.nudZoom.ValueChanged += new System.EventHandler(this.numericUpDown2_ValueChanged);
            // 
            // lblZoom
            // 
            this.lblZoom.Location = new System.Drawing.Point(238, 0);
            this.lblZoom.Name = "lblZoom";
            this.lblZoom.Size = new System.Drawing.Size(36, 23);
            this.lblZoom.TabIndex = 2;
            this.lblZoom.Text = "Zoom";
            this.lblZoom.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // pPreview
            // 
            this.pPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.pPreview.AutoScroll = true;
            this.pPreview.BackColor = System.Drawing.SystemColors.ControlDarkDark;
            this.pPreview.Controls.Add(this.pbPreviewImage);
            this.pPreview.Location = new System.Drawing.Point(0, 26);
            this.pPreview.Name = "pPreview";
            this.pPreview.Size = new System.Drawing.Size(533, 365);
            this.pPreview.TabIndex = 3;
            // 
            // pbPreviewImage
            // 
            this.pbPreviewImage.Location = new System.Drawing.Point(52, 6);
            this.pbPreviewImage.Name = "pbPreviewImage";
            this.pbPreviewImage.Size = new System.Drawing.Size(250, 339);
            this.pbPreviewImage.TabIndex = 2;
            this.pbPreviewImage.TabStop = false;
            // 
            // flowLayoutPanelLeft
            // 
            this.flowLayoutPanelLeft.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.flowLayoutPanelLeft.Controls.Add(this.toolStrip1);
            this.flowLayoutPanelLeft.Location = new System.Drawing.Point(4, 0);
            this.flowLayoutPanelLeft.Name = "flowLayoutPanelLeft";
            this.flowLayoutPanelLeft.Size = new System.Drawing.Size(266, 25);
            this.flowLayoutPanelLeft.TabIndex = 4;
            // 
            // toolStrip1
            // 
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.printToolStripMenuItem,
            this.printSettingToolStripMenuItem,
            this.pageSettingsToolStripMenuItem,
            this.closeToolStripMenuItem});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(266, 25);
            this.toolStrip1.TabIndex = 0;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // printToolStripMenuItem
            // 
            this.printToolStripMenuItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.printToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("printToolStripMenuItem.Image")));
            this.printToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.printToolStripMenuItem.Name = "printToolStripMenuItem";
            this.printToolStripMenuItem.Size = new System.Drawing.Size(33, 22);
            this.printToolStripMenuItem.Text = "Print";
            this.printToolStripMenuItem.Click += new System.EventHandler(this.printToolStripMenuItem_Click);
            // 
            // pageSettingsToolStripMenuItem
            // 
            this.pageSettingsToolStripMenuItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.pageSettingsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.marginToolStripMenuItem,
            this.orientationToolStripMenuItem});
            this.pageSettingsToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("pageSettingsToolStripMenuItem.Image")));
            this.pageSettingsToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.pageSettingsToolStripMenuItem.Name = "pageSettingsToolStripMenuItem";
            this.pageSettingsToolStripMenuItem.Size = new System.Drawing.Size(86, 22);
            this.pageSettingsToolStripMenuItem.Text = "Page Settings";
            // 
            // marginToolStripMenuItem
            // 
            this.marginToolStripMenuItem.Name = "marginToolStripMenuItem";
            this.marginToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.marginToolStripMenuItem.Text = "Margin";
            this.marginToolStripMenuItem.Click += new System.EventHandler(this.marginToolStripMenuItem_Click);
            // 
            // orientationToolStripMenuItem
            // 
            this.orientationToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.portraitToolStripMenuItem,
            this.landscapeToolStripMenuItem});
            this.orientationToolStripMenuItem.Name = "orientationToolStripMenuItem";
            this.orientationToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.orientationToolStripMenuItem.Text = "Orientation";
            // 
            // portraitToolStripMenuItem
            // 
            this.portraitToolStripMenuItem.Name = "portraitToolStripMenuItem";
            this.portraitToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
            this.portraitToolStripMenuItem.Text = "Portrait";
            this.portraitToolStripMenuItem.Click += new System.EventHandler(this.portraitToolStripMenuItem_Click);
            // 
            // landscapeToolStripMenuItem
            // 
            this.landscapeToolStripMenuItem.Name = "landscapeToolStripMenuItem";
            this.landscapeToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
            this.landscapeToolStripMenuItem.Text = "Landscape";
            this.landscapeToolStripMenuItem.Click += new System.EventHandler(this.landscapeToolStripMenuItem_Click);
            // 
            // closeToolStripMenuItem
            // 
            this.closeToolStripMenuItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.closeToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("closeToolStripMenuItem.Image")));
            this.closeToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.closeToolStripMenuItem.Name = "closeToolStripMenuItem";
            this.closeToolStripMenuItem.Size = new System.Drawing.Size(37, 22);
            this.closeToolStripMenuItem.Text = "Close";
            this.closeToolStripMenuItem.Click += new System.EventHandler(this.closeToolStripMenuItem_Click);
            // 
            // printSettingToolStripMenuItem
            // 
            this.printSettingToolStripMenuItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.printSettingToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fitSettingsToolStripMenuItem,
            this.selectedPrinterToolStripMenuItem});
            this.printSettingToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("printSettingToolStripMenuItem.Image")));
            this.printSettingToolStripMenuItem.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.printSettingToolStripMenuItem.Name = "printSettingToolStripMenuItem";
            this.printSettingToolStripMenuItem.Size = new System.Drawing.Size(79, 22);
            this.printSettingToolStripMenuItem.Text = "Print Setting";
            // 
            // fitSettingsToolStripMenuItem
            // 
            this.fitSettingsToolStripMenuItem.Name = "fitSettingsToolStripMenuItem";
            this.fitSettingsToolStripMenuItem.Size = new System.Drawing.Size(161, 22);
            this.fitSettingsToolStripMenuItem.Text = "Fit Settings";
            this.fitSettingsToolStripMenuItem.Click += new System.EventHandler(this.fitSettingsToolStripMenuItem_Click);
            // 
            // selectedPrinterToolStripMenuItem
            // 
            this.selectedPrinterToolStripMenuItem.Name = "selectedPrinterToolStripMenuItem";
            this.selectedPrinterToolStripMenuItem.Size = new System.Drawing.Size(161, 22);
            this.selectedPrinterToolStripMenuItem.Text = "Selected Printer";
            // 
            // GLPrintPreviewControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoScroll = true;
            this.Controls.Add(this.flowLayoutPanelLeft);
            this.Controls.Add(this.pPreview);
            this.Controls.Add(this.flowLayoutPanelRight);
            this.Name = "GLPrintPreviewControl";
            this.Size = new System.Drawing.Size(533, 391);
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
            this.flowLayoutPanelRight.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.nudZoom)).EndInit();
            this.pPreview.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pbPreviewImage)).EndInit();
            this.flowLayoutPanelLeft.ResumeLayout(false);
            this.flowLayoutPanelLeft.PerformLayout();
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.NumericUpDown numericUpDown1;
        private System.Windows.Forms.FlowLayoutPanel flowLayoutPanelRight;
        private System.Windows.Forms.NumericUpDown nudZoom;
        private System.Windows.Forms.Label lblZoom;
        private System.Windows.Forms.Label lblPage;
        private System.Windows.Forms.Panel pPreview;
        private System.Windows.Forms.PictureBox pbPreviewImage;
        private System.Windows.Forms.FlowLayoutPanel flowLayoutPanelLeft;
        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.ToolStripDropDownButton pageSettingsToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem marginToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem orientationToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem portraitToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem landscapeToolStripMenuItem;
        private System.Windows.Forms.ToolStripButton printToolStripMenuItem;
        private System.Windows.Forms.ToolStripButton closeToolStripMenuItem;
        private System.Windows.Forms.ToolStripDropDownButton printSettingToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem fitSettingsToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem selectedPrinterToolStripMenuItem;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Mediatech Solutions
Italy Italy
I’m an IT Project Manager for an Italian Betting Company and over the last 2 years I acquired experience in Betting area.
I have developed code in different object oriented languages (C#, C++, Java) for more than 10 years using a set of technology such as .Net, J2EE, multithreading, etc…

Comments and Discussions