Click here to Skip to main content
15,886,075 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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using GridPrintPreviewLib;

namespace TestApp
{
    class MyGridDoc : GridPrintDocument
    {
        public MyGridDoc(DataGridView source, Font docFont, bool flagHeader)
            : base(source, docFont, flagHeader)
        {
        }

        protected override void onDrawCell(System.Drawing.Graphics g, string s, System.Drawing.RectangleF layoutRect, System.Drawing.StringFormat format, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.Brush brushBack, System.Windows.Forms.DataGridViewAdvancedBorderStyle borderStyle)
        {
            borderStyle.Left = System.Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single;
            borderStyle.Right = System.Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single;
            base.onDrawCell(g, s, layoutRect, format, font, brush, brushBack, borderStyle);
        }

        protected override void onDrawColumnHeader(System.Drawing.Graphics g, string s, System.Drawing.RectangleF layoutRect, System.Drawing.StringFormat format, System.Drawing.Font font, System.Windows.Forms.DataGridViewAdvancedBorderStyle borderStyle)
        {
            borderStyle.All = System.Windows.Forms.DataGridViewAdvancedCellBorderStyle.Single;
            base.onDrawColumnHeader(g, s, layoutRect, format, font, borderStyle);
        }
    }
}

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