Click here to Skip to main content
13,863,642 members
Click here to Skip to main content
Add your own
alternative version


79 bookmarked
Posted 19 Sep 2004

DataGrid Printing Class V1.0b

, 27 Feb 2006
Rate this:
Please Sign up or sign in to vote.
A DataGrid printing class.

UML Design

Sample Image


Sample Image


The DataGrid control is one of the worst things to work with, yet very needed. The real problem about this control is printing the grid. This class supports RightToLeft printing, colored and aligned grid printing...


I had to do a project that supports RightToLeft grid printing, but all the printing classes I found just didn't do the job. So, I made a new one...


The main class is PrinterClass, it does the actual print. Grid is a class holding the DataGrid data, it's made of Cells and Headers. Cell represents a single cell in a grid with Location, Font Alignment etc. Header represents a single header cell (inherits Cell).

Using the code

Grid Creation

public Grid(DataGrid TheGrid)
        //get the Data in the grid
        DataTable TableGrid = (DataTable)TheGrid.DataSource;

        //set number of rows
        rows = TableGrid.Rows.Count;

        //set number of columns
        //first check if the grid has tablestyle and columnstyle


        if (TheGrid.TableStyles[TableGrid.TableName].GridColumnStyles.Count>0)
            columns = 

        //init number of columns to headers
        Headers = new Header[Columns];


        Cells = new Cell[Rows,Columns];

        //Copy Cells
        for (int i=0;i<Rows;i++)
            for (int j=0;j<Columns;j++)
                Cells[i,j] = new Cell(i, j, TheGrid.Font, 
                   TheGrid.GetCellBounds(i,j), TheGrid[i,j].ToString());

        //define grid colors
    catch (Exception e)

Grid Printing

private void PrintDataGrid(Graphics g)
    StringFormat sf = new StringFormat();

    //if we want to print the grid right to left
    if (bRightToLeft)
        CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width - 

        sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
        CurrentX = PrintDoc.DefaultPageSettings.Margins.Left;

    for (int i=0;i<PrintGrid.Rows;i++)
        for (int j=0;j<PrintGrid.Columns;j++)
            //set cell alignment
            switch (PrintGrid[i,j].Alignment)
                case HorizontalAlignment.Left:
                    sf.Alignment = StringAlignment.Near;

                case HorizontalAlignment.Center:
                    sf.Alignment = StringAlignment.Center;

                case HorizontalAlignment.Right:
                    sf.Alignment = StringAlignment.Far;

            //advance X according to order
            if (bRightToLeft)
                //draw the cell bounds (lines)
                g.DrawRectangle(Pens.Black,CurrentX - PrintGrid[i,j].Width,

                //draw the cell text
                   new RectangleF(CurrentX - PrintGrid[i,j].Width, 
                   CurrentY, PrintGrid[i,j].Width,

                //next cell
                CurrentX -=PrintGrid[i,j].Width;

                //draw the cell bounds (lines)
                g.DrawRectangle(Pens.Black ,CurrentX,CurrentY, 

                //draw the cell text
                //Draw text by alignment
                     Brushes.Black, new RectangleF(CurrentX,CurrentY, 

                //next cell
                CurrentX +=PrintGrid[i,j].Width;


        //reset to beginning
        if (bRightToLeft)
            //right align
            CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width - 
            //left align
            CurrentX = PrintDoc.DefaultPageSettings.Margins.Left;

        //advance to next row
        CurrentY = CurrentY + PrintGrid[i,0].Height;

Current Todo:

  1. Fix - When grid is empty, header is not printed well.
  2. When grid is lager than the WIDTH of the page, continue to next.

Other DataGrid Printing Classes


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Syed Javed11-Jan-10 19:53
memberSyed Javed11-Jan-10 19:53 
Generalexcellent + small thing Pin
xor.be15-Nov-07 0:37
memberxor.be15-Nov-07 0:37 
Questioninvoice datagrid printing? Pin
geee27-Jun-07 16:56
membergeee27-Jun-07 16:56 
AnswerRe: invoice datagrid printing? Pin
nashcontrol27-Jun-07 22:44
membernashcontrol27-Jun-07 22:44 
GeneralRe: invoice datagrid printing? Pin
geee27-Jun-07 22:54
membergeee27-Jun-07 22:54 
GeneralPreview Shows Empty Page Pin
scptech5-May-07 6:38
memberscptech5-May-07 6:38 
GeneralRe: Preview Shows Empty Page Pin
nashcontrol5-May-07 12:34
membernashcontrol5-May-07 12:34 Pin
HRiazi3-Dec-06 22:14
memberHRiazi3-Dec-06 22:14 
GeneralDataGridColumnStyles Problem Pin
clueless2326-Nov-06 22:24
memberclueless2326-Nov-06 22:24 
GeneralPrintings cell values formatted like in datagrid Pin
Senol Akbulak28-Jul-06 5:41
memberSenol Akbulak28-Jul-06 5:41 
QuestionProblem in print preview [modified] Pin
v_sadeghpour14-Jun-06 14:07
memberv_sadeghpour14-Jun-06 14:07 
GeneralGrid Column Styles does not work Pin
project_manager14-Jun-06 1:14
memberproject_manager14-Jun-06 1:14 
GeneralNull text does not print Pin
project_manager13-Jun-06 20:52
memberproject_manager13-Jun-06 20:52 
Generalfit the datagrid Pin
Trimix28-Feb-06 16:29
memberTrimix28-Feb-06 16:29 
GeneralI don't understand Pin
nashcontrol9-Mar-06 11:22
membernashcontrol9-Mar-06 11:22 
GeneralConversion to DataGridView Pin
Joe Sonderegger23-Feb-06 3:12
memberJoe Sonderegger23-Feb-06 3:12 
GeneralRe: Conversion to DataGridView Pin
Pink Floyd14-Mar-06 11:17
memberPink Floyd14-Mar-06 11:17 
GeneralRe: Conversion to DataGridView Pin
lilyminako14-Mar-06 16:08
memberlilyminako14-Mar-06 16:08 
GeneralRe: Conversion to DataGridView Pin
Emile van Gerwen14-Mar-06 23:47
memberEmile van Gerwen14-Mar-06 23:47 
GeneralRe: Conversion to DataGridView Pin
mariadelapaz27-Mar-06 10:40
membermariadelapaz27-Mar-06 10:40 
GeneralRe: Conversion to DataGridView Pin
Waruna Rajapakse6-Jun-06 0:45
memberWaruna Rajapakse6-Jun-06 0:45 
GeneralRe: Conversion to DataGridView Pin
karan_23_j11-Jun-06 20:54
memberkaran_23_j11-Jun-06 20:54 
GeneralRe: Conversion to DataGridView Pin
karan_23_j11-Jun-06 20:48
memberkaran_23_j11-Jun-06 20:48 
GeneralRe: Conversion to DataGridView Pin
Joe Sonderegger13-Jul-06 5:58
memberJoe Sonderegger13-Jul-06 5:58 
GeneralActual print doesn't how data grid Pin
yarowave12-Feb-06 16:34
memberyarowave12-Feb-06 16:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190214.1 | Last Updated 27 Feb 2006
Article Copyright 2004 by nashcontrol
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid