Click here to Skip to main content
15,905,781 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 272.8K   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: Actual print doesn't how data grid Pin
Joe Sonderegger23-Feb-06 0:57
Joe Sonderegger23-Feb-06 0:57 
Generalcheck out updated code Pin
nashcontrol25-Feb-06 6:27
nashcontrol25-Feb-06 6:27 
GeneralRe: check out updated code Pin
yarowave25-Feb-06 20:55
yarowave25-Feb-06 20:55 
Questioncan print across Pin
cydus15-Jan-06 6:49
cydus15-Jan-06 6:49 
Answernot implemented yet Pin
nashcontrol26-Jan-06 10:32
nashcontrol26-Jan-06 10:32 
Generalmissing letters at the end of headers Pin
think304-Nov-05 5:02
think304-Nov-05 5:02 
GeneralRe: missing letters at the end of headers Pin
Calvin K21-Mar-06 7:24
Calvin K21-Mar-06 7:24 
QuestionMultiline Datagrid? Pin
| Muhammad Waqas Butt |4-Sep-05 22:09
professional| Muhammad Waqas Butt |4-Sep-05 22:09 
Hi,

How are you? I have use you demo but i didn't see multiline datagrid function in it or loook. Please tell me how to do multiline in it and where the code must be written.

I am facing some problems in C#.net, which are really distributing me. I’m sending you a sample of code (sample file includes C# Source files, Crystal Report and MS Access DB) in zip form. You download this file and see, where I do wrong and please tell me the solution of it. I have written down the problems in C# file and in the bottom of email as well. Please get me out of this. I hope you will help me in this and if you have any question or need any thing regard this please contact me through email. In the end I again request you because it has much importance in my programming.



Problems:

(1) How to get dynamic “Crystal Report” data I mean I have 3 tables named as Job, JobDesc and Emp_Info’ there is a primary key name JobNo now I want that I pass sqldb command at run time and in where clause I pass the JobNo so then on the crystal report only data show which have given JobNo.
(2) How to set Multi-line Report in Crystal Report Detail Section. Like this:
(3) How to display custom date format (Sep-06-2005) in Crystal Report.

Job No Title Employee Id
1 Dummy Title Dummy Title Dummy Title Dummy Title Dummy Title Dummy Title 001
2 Dummy Title Dummy Title Dummy 002
3 Dummy Title Dummy Title Dummy Title Dummy Title Dummy Title Dummy Title Dummy Title Dummy Title 003

I mean which property I have to change in order to get this kind of look.

Yours sincerely,

Muhammad Waqas Butt
waqasb4all@yahoo.com

|Muhamad Waqas Butt|
waqasb4all@yahoo.com
www.sktech.freewebspace.com
AnswerRe: Multiline Datagrid? Pin
nashcontrol5-Sep-05 2:15
nashcontrol5-Sep-05 2:15 
Question[Message Deleted] Pin
| Muhammad Waqas Butt |5-Sep-05 5:33
professional| Muhammad Waqas Butt |5-Sep-05 5:33 
GeneralRe: Multiline Datagrid? Pin
| Muhammad Waqas Butt |5-Sep-05 5:47
professional| Muhammad Waqas Butt |5-Sep-05 5:47 
GeneralRe: Multiline Datagrid? Pin
nashcontrol5-Sep-05 16:17
nashcontrol5-Sep-05 16:17 
GeneralRe: Multiline Datagrid? Pin
| Muhammad Waqas Butt |22-Sep-05 6:43
professional| Muhammad Waqas Butt |22-Sep-05 6:43 
Generalread instructions.... Pin
nashcontrol30-Sep-05 4:23
nashcontrol30-Sep-05 4:23 
AnswerRe: Multiline Datagrid? Pin
Marcello Miorelli21-Apr-06 4:10
Marcello Miorelli21-Apr-06 4:10 
GeneralLandscape option Pin
kessaf24-Aug-05 1:28
kessaf24-Aug-05 1:28 
Generalgreat! Pin
nashcontrol5-Sep-05 2:20
nashcontrol5-Sep-05 2:20 
AnswerRe: great! Pin
Marcello Miorelli21-Apr-06 4:12
Marcello Miorelli21-Apr-06 4:12 
GeneralGreat Pin
dosz8-Aug-05 23:40
dosz8-Aug-05 23:40 
GeneralI cannot see/print headers in preview Pin
idelix31-Jul-05 8:41
idelix31-Jul-05 8:41 
Generaldataview rowfilter Pin
Member 159661520-Jul-05 7:46
Member 159661520-Jul-05 7:46 
Generalthanks for the notice! Pin
nashcontrol5-Sep-05 2:23
nashcontrol5-Sep-05 2:23 
QuestionHow can I print a range of pages? Pin
GuruDK30-Jun-05 23:16
GuruDK30-Jun-05 23:16 
AnswerRe: How can I print a range of pages? Pin
nashcontrol25-Feb-06 6:28
nashcontrol25-Feb-06 6:28 
GeneralRe: How can I print a range of pages? Pin
clueless2325-Mar-06 22:49
clueless2325-Mar-06 22:49 

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.