Click here to Skip to main content
15,891,375 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.5K   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.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace GridPrintPreviewLib
{
    /// <summary>
    /// Control to manage margins setting with preview
    /// </summary>
    public partial class GLMarginSettings : UserControl
    {
        // Maring and Size are stored in display unit
        private Margins m_PageMargins = new Margins(0, 0 , 0, 0);
        private PaperSize m_PageSize = new PaperSize();
        // Unit used for show margin to user
        private PrinterUnit m_UnitForShow = PrinterUnit.Display;
        // Flag to check if page is in landscape
        private bool m_Landscape = false;

        public GLMarginSettings()
        {
            InitializeComponent();
        }

        /// <summary>
        /// PageSize in display unit
        /// </summary>
        public PaperSize PageSize
        {
            get
            {
                return m_PageSize;
            }
            set
            {
                m_PageSize = value;
                RefreshPreview();
            }
        }

        /// <summary>
        /// Margins in display unit
        /// </summary>
        public Margins PageMargins
        {
            get
            {
                return m_PageMargins;
            }
            set
            {
                m_PageMargins = value;
                RefreshPreview();
                RefreshUnit();
            }
        }

        /// <summary>
        /// Unit used for show values to user
        /// </summary>
        public PrinterUnit ShowUnit
        {
            get
            {
                return m_UnitForShow;
            }
            set
            {
                m_UnitForShow = value;
                RefreshUnit();
            }
        }

        /// <summary>
        /// Get/Set if page in is landscape
        /// </summary>
        public bool Landscape
        {
            get
            {
                return m_Landscape;
            }
            set
            {
                m_Landscape = value;
                RefreshPreview();
            }
        }

        /// <summary>
        /// Refresh image of the page with margin
        /// </summary>
        private void RefreshPreview()
        {
            if(m_PageSize.Width == 0 || m_PageSize.Height == 0)
            {
                return;
            }
            if (pbPage.Image == null)
            {
                Bitmap bitmap = new Bitmap(pbPage.Width, pbPage.Height);
                pbPage.Image = bitmap;
            }
            using (Graphics g = Graphics.FromImage(pbPage.Image))
            {
                float pageWidth = (!m_Landscape) ? m_PageSize.Width : m_PageSize.Height;
                float pageHeight = (!m_Landscape) ? m_PageSize.Height : m_PageSize.Width;
                float scaleX = pbPage.Width / pageWidth;
                float scaleY = pbPage.Height / pageHeight;
                float scale = Math.Min(scaleX, scaleY);
                SizeF scalePageSize = new SizeF(pageWidth * scale, pageHeight * scale);
                float x = (pbPage.Width - scalePageSize.Width) / 2;
                float y = (pbPage.Height - scalePageSize.Height) / 2;
                g.FillRectangle(Brushes.Gray, 0, 0, pbPage.Width, pbPage.Height);
                g.FillRectangle(Brushes.White, x, y, scalePageSize.Width, scalePageSize.Height);
                Pen p = new Pen(Color.Black, 1);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                float mleft = m_PageMargins.Left * scaleX + x;
                g.DrawLine(p, mleft, 0, mleft, pbPage.Height);
                float mright = x + scalePageSize.Width - (m_PageMargins.Right * scaleX);
                g.DrawLine(p, mright, 0, mright, pbPage.Height);
                float mtop = m_PageMargins.Top * scaleY + y;
                g.DrawLine(p, 0, mtop, pbPage.Width, mtop);
                float mbottom = y + scalePageSize.Height - (m_PageMargins.Bottom * scaleY);
                g.DrawLine(p, 0, mbottom, pbPage.Width, mbottom);
            }
            this.pbPage.Refresh();
        }

        /// <summary>
        /// Refresh unit shown to user
        /// </summary>
        private void RefreshUnit()
        {
            Margins m = TransformHelper.Convert(m_PageMargins, PrinterUnit.Display, m_UnitForShow);
            nudLeft.Value = m.Left;
            nudTop.Value = m.Top;
            nudRight.Value = m.Right;
            nudBottom.Value = m.Bottom;
        }

        /// <summary>
        /// Handle numeric up down change
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nud_ValueChanged(object sender, EventArgs e)
        {
            Margins m = new Margins((int)nudLeft.Value, (int)nudRight.Value, (int)nudTop.Value, (int)nudBottom.Value);
            m_PageMargins = TransformHelper.Convert(m, m_UnitForShow, PrinterUnit.Display);
            this.RefreshPreview();
        }
    }
}

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