Click here to Skip to main content
6,596,602 members and growing! (20,705 online)
Email Password   helpLost your password?
Desktop Development » Grid & Data Controls » Grid controls     Intermediate

DataGrid Printing Class V1.0b

By nashcontrol

A DataGrid printing class.
C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
Posted:19 Sep 2004
Updated:27 Feb 2006
Views:148,357
Bookmarked:68 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 5.17 Rating: 4.04 out of 5
1 vote, 5.3%
1

2
3 votes, 15.8%
3
8 votes, 42.1%
4
7 votes, 36.8%
5

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

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

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

About the Author

nashcontrol


Member

Occupation: Web Developer
Company: Indigo - Smart House Solutions
Location: Israel Israel

Other popular Grid & Data Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 70 (Total in Forum: 70) (Refresh)FirstPrevNext
Generalexcellent + small thing Pinmemberxor.be0:37 15 Nov '07  
Generalinvoice datagrid printing? Pinmembergeee16:56 27 Jun '07  
GeneralRe: invoice datagrid printing? Pinmembernashcontrol22:44 27 Jun '07  
GeneralRe: invoice datagrid printing? Pinmembergeee22:54 27 Jun '07  
GeneralPreview Shows Empty Page Pinmemberscptech6:38 5 May '07  
GeneralRe: Preview Shows Empty Page Pinmembernashcontrol12:34 5 May '07  
Questionasp.net PinmemberH.Riazi22:14 3 Dec '06  
GeneralDataGridColumnStyles Problem Pinmemberclueless2322:24 26 Nov '06  
GeneralPrintings cell values formatted like in datagrid PinmemberSenol Akbulak5:41 28 Jul '06  
QuestionProblem in print preview [modified] Pinmemberv_sadeghpoor14:07 14 Jun '06  
GeneralGrid Column Styles does not work Pinmemberproject_manager1:14 14 Jun '06  
GeneralNull text does not print Pinmemberproject_manager20:52 13 Jun '06  
Generalfit the datagrid PinmemberTrimix16:29 28 Feb '06  
GeneralI don't understand Pinmembernashcontrol11:22 9 Mar '06  
GeneralConversion to DataGridView PinmemberNice Life3:12 23 Feb '06  
GeneralRe: Conversion to DataGridView PinmemberPink Floyd11:17 14 Mar '06  
GeneralRe: Conversion to DataGridView Pinmemberlilyminako16:08 14 Mar '06  
GeneralRe: Conversion to DataGridView PinmemberDjeez23:47 14 Mar '06  
GeneralRe: Conversion to DataGridView Pinmembermariadelapaz10:40 27 Mar '06  
GeneralRe: Conversion to DataGridView PinmemberWaruna Rajapakse0:45 6 Jun '06  
GeneralRe: Conversion to DataGridView Pinmemberkaran_23_j20:54 11 Jun '06  
GeneralRe: Conversion to DataGridView Pinmemberkaran_23_j20:48 11 Jun '06  
GeneralRe: Conversion to DataGridView PinmemberNice Life5:58 13 Jul '06  
GeneralActual print doesn't how data grid Pinmemberyarowave16:34 12 Feb '06  
GeneralRe: Actual print doesn't how data grid PinmemberNice Life1:57 23 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Feb 2006
Editor: Smitha Vijayan
Copyright 2004 by nashcontrol
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project