Click here to Skip to main content
15,885,944 members
Articles / Programming Languages / C#
Article

DataGrid Printing Class V1.0b

Rate me:
Please Sign up or sign in to vote.
4.16/5 (21 votes)
27 Feb 2006 271.9K   2.6K   79   71
A DataGrid printing class.

UML Design

Sample Image

Preview

Sample Image

Introduction

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...

Background

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...

Implementation

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

C#
public Grid(DataGrid TheGrid)
{
    try
    {
        //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

        CreateColumnStyles(TheGrid,TableGrid);

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

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

        SetHeaders(TheGrid,TableGrid);

        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
        SetColors(TheGrid);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

Grid Printing

C#
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 - 
        PrintDoc.DefaultPageSettings.Margins.Right;

        sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
    }
    else
    {
        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)
            {
                    //left
                case HorizontalAlignment.Left:
                    sf.Alignment = StringAlignment.Near;
                    break;

                    //center
                case HorizontalAlignment.Center:
                    sf.Alignment = StringAlignment.Center;
                    break;

                    //right
                case HorizontalAlignment.Right:
                    sf.Alignment = StringAlignment.Far;
                    break;
            }

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

                //draw the cell text
                g.DrawString(PrintGrid[i,j].Text,
                   PrintGrid[i,j].Font,Brushes.Black,
                   new RectangleF(CurrentX - PrintGrid[i,j].Width, 
                   CurrentY, PrintGrid[i,j].Width,
                   PrintGrid[i,j].Height),sf);

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

            }
            else
            {
                //draw the cell bounds (lines)
                g.DrawRectangle(Pens.Black ,CurrentX,CurrentY, 
                     PrintGrid[i,j].Width,PrintGrid[i,j].Height);

                //draw the cell text
                //Draw text by alignment
                    
                g.DrawString(PrintGrid[i,j].Text,PrintGrid[i,j].Font, 
                     Brushes.Black, new RectangleF(CurrentX,CurrentY, 
                     PrintGrid[i,j].Width,PrintGrid[i,j].Height),sf);

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

        }

        //reset to beginning
        if (bRightToLeft)
        {
            //right align
            CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width - 
                       PrintDoc.DefaultPageSettings.Margins.Right;
        }
        else
        {
            //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

License

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


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Good Work Pin
nashcontrol20-May-05 2:42
nashcontrol20-May-05 2:42 
GeneralRe: Good Work Pin
Nidhi S27-May-05 2:03
Nidhi S27-May-05 2:03 
GeneralNice but print not as expected Pin
Chris_Green13-May-05 5:32
Chris_Green13-May-05 5:32 
GeneralGreat and complete code!! Pin
G_Montesano13-Apr-05 3:00
G_Montesano13-Apr-05 3:00 
GeneralHELP:: migrated vb didn't work Pin
Anonymous28-Feb-05 10:15
Anonymous28-Feb-05 10:15 
GeneralRe: HELP:: migrated vb didn't work Pin
Nidhi S15-May-05 19:00
Nidhi S15-May-05 19:00 
Generaldata grid control Pin
Anonymous29-Dec-04 0:59
Anonymous29-Dec-04 0:59 
GeneralPrint preview is very slow Pin
gabis21-Nov-04 20:23
gabis21-Nov-04 20:23 
Print preview takes forever Cry | :(( for such a small grid.
Generalit prints only the header! Pin
salontec10-Nov-04 5:42
salontec10-Nov-04 5:42 
GeneralRe: it prints only the header! Pin
nashcontrol11-Nov-04 1:33
nashcontrol11-Nov-04 1:33 
GeneralRe: it prints only the header! Pin
zliang6829-Nov-04 13:48
zliang6829-Nov-04 13:48 
GeneralRe: it prints only the header! Pin
zliang6829-Nov-04 14:26
zliang6829-Nov-04 14:26 
GeneralRe: it prints only the header! Pin
zliang6830-Nov-04 11:30
zliang6830-Nov-04 11:30 
Generalprinting from print preview Pin
cromulent5-Nov-04 10:56
cromulent5-Nov-04 10:56 
Generalfeedback wanted Pin
nashcontrol29-Sep-04 3:25
nashcontrol29-Sep-04 3:25 
GeneralRe: feedback wanted Pin
hsee14-Nov-04 23:49
hsee14-Nov-04 23:49 
GeneralRe: feedback wanted Pin
Member 56840127-Feb-05 23:35
Member 56840127-Feb-05 23:35 

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.