Click here to Skip to main content
Click here to Skip to main content

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
Follow on   Twitter

Comments and Discussions

GeneralMy vote of 1 PinmemberSyed Javed11-Jan-10 18:53 
Generalexcellent + small thing Pinmemberxor.be14-Nov-07 23:37 
Questioninvoice datagrid printing? Pinmembergeee27-Jun-07 15:56 
AnswerRe: invoice datagrid printing? Pinmembernashcontrol27-Jun-07 21:44 
GeneralRe: invoice datagrid printing? Pinmembergeee27-Jun-07 21:54 
GeneralPreview Shows Empty Page Pinmemberscptech5-May-07 5:38 
GeneralRe: Preview Shows Empty Page Pinmembernashcontrol5-May-07 11:34 PinmemberH.Riazi3-Dec-06 21:14 
GeneralDataGridColumnStyles Problem Pinmemberclueless2326-Nov-06 21:24 
GeneralPrintings cell values formatted like in datagrid PinmemberSenol Akbulak28-Jul-06 4:41 
QuestionProblem in print preview [modified] Pinmemberv_sadeghpoor14-Jun-06 13:07 
GeneralGrid Column Styles does not work Pinmemberproject_manager14-Jun-06 0:14 
GeneralNull text does not print Pinmemberproject_manager13-Jun-06 19:52 
Generalfit the datagrid PinmemberTrimix28-Feb-06 15:29 
GeneralI don't understand Pinmembernashcontrol9-Mar-06 10:22 
GeneralConversion to DataGridView PinmemberNice Life23-Feb-06 2:12 
GeneralRe: Conversion to DataGridView PinmemberPink Floyd14-Mar-06 10:17 
GeneralRe: Conversion to DataGridView Pinmemberlilyminako14-Mar-06 15:08 
GeneralRe: Conversion to DataGridView PinmemberDjeez14-Mar-06 22:47 
GeneralRe: Conversion to DataGridView Pinmembermariadelapaz27-Mar-06 9:40 
GeneralRe: Conversion to DataGridView PinmemberWaruna Rajapakse5-Jun-06 23:45 
GeneralRe: Conversion to DataGridView Pinmemberkaran_23_j11-Jun-06 19:54 
GeneralRe: Conversion to DataGridView Pinmemberkaran_23_j11-Jun-06 19:48 
GeneralRe: Conversion to DataGridView PinmemberNice Life13-Jul-06 4:58 
GeneralActual print doesn't how data grid Pinmemberyarowave12-Feb-06 15:34 
GeneralRe: Actual print doesn't how data grid PinmemberNice Life23-Feb-06 0:57 
Generalcheck out updated code Pinmembernashcontrol25-Feb-06 6:27 
GeneralRe: check out updated code Pinmemberyarowave25-Feb-06 20:55 
Questioncan print across Pinmembercydus15-Jan-06 6:49 
Answernot implemented yet Pinmembernashcontrol26-Jan-06 10:32 
Generalmissing letters at the end of headers Pinmemberthink304-Nov-05 5:02 
GeneralRe: missing letters at the end of headers PinmemberCalvin K21-Mar-06 7:24 
QuestionMultiline Datagrid? PinmemberMuhammad Waqas Butt4-Sep-05 22:09 
AnswerRe: Multiline Datagrid? Pinmembernashcontrol5-Sep-05 2:15 
Question[Msg Deleted] PinmemberMuhammad Waqas Butt5-Sep-05 5:33 
GeneralRe: Multiline Datagrid? PinmemberMuhammad Waqas Butt5-Sep-05 5:47 
GeneralRe: Multiline Datagrid? Pinmembernashcontrol5-Sep-05 16:17 
GeneralRe: Multiline Datagrid? PinmemberMuhammad Waqas Butt22-Sep-05 6:43 
Generalread instructions.... Pinmembernashcontrol30-Sep-05 4:23 
AnswerRe: Multiline Datagrid? PinmemberMarcelo Miorelli21-Apr-06 4:10 
GeneralLandscape option Pinmemberkessaf24-Aug-05 1:28 
Generalgreat! Pinmembernashcontrol5-Sep-05 2:20 
AnswerRe: great! PinmemberMarcelo Miorelli21-Apr-06 4:12 
GeneralGreat Pinmemberdosz8-Aug-05 23:40 
GeneralI cannot see/print headers in preview Pinmemberidelix31-Jul-05 8:41 
Generaldataview rowfilter PinmemberRaimondo Giuliani20-Jul-05 7:46 
Generalthanks for the notice! Pinmembernashcontrol5-Sep-05 2:23 
QuestionHow can I print a range of pages? PinmemberGuruDK30-Jun-05 23:16 
AnswerRe: How can I print a range of pages? Pinmembernashcontrol25-Feb-06 6:28 
GeneralRe: How can I print a range of pages? Pinmemberclueless2325-Mar-06 22:49 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 27 Feb 2006
Article Copyright 2004 by nashcontrol
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid