Click here to Skip to main content
11,721,671 members (81,185 online)
Click here to Skip to main content

DataGrid Printing Class v1.0b

, 31 May 2005 196.9K 3.2K 60
Rate this:
Please Sign up or sign in to vote.
A DataGrid printing class.

Sample Image - UML_Design.jpg

UML design

Sample Image - Preview.jpg

Preview

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 using VB.NET, but all the printing classes I found just didn't do the job. I found a project but it was written using C# and I had to write the project using VB.NET. So I have rewritten the same project in DataGrid Printing Class V1.0b by nashcontrol in VB.NET.

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: Creating the Constructor for the Grid

Public Sub New(ByVal TheGrid As DataGrid)
  Try
      '//get the Data in the grid
      Dim TableGrid As DataTable = Nothing
      If (TheGrid.DataSource.GetType() Is GetType(DataView)) Then
          Dim ViewGrid As DataView = CType(TheGrid.DataSource, DataView)
          TableGrid = ViewGrid.Table
      Else
          TableGrid = CType(TheGrid.DataSource, DataTable)
      End If

      '//set number of rows
      row = TableGrid.Rows.Count

      'set number of columns
      'first check if the grid has tablestyle and columnstyle
      'check for table styles
      If (TheGrid.TableStyles.Count = 0) Then
          '//create table style and column style
          CreateColumnStyles(TheGrid, TableGrid)
      Else
          '//create column styles if there are none
          If (TheGrid.TableStyles(TableGrid.TableName).GridColumnStyles.Count=0) Then
              CreateColumnStyles(TheGrid, TableGrid
          End If
      End If

      '//set number of columns
      column = TheGrid.TableStyles(TableGrid.TableName).GridColumnStyles.Count
      Cells = New Cell(Rows, Columns) {}
      'Copy Cells
      Dim i As Integer
      For i = 0 To Rows - 1
          Dim j As Integer
          For j = 0 To Columns - 1
            Cells(i, j) = New Cell(i, j, TheGrid.Font, _
              TheGrid.TableStyles(TableGrid.TableName).GridColumnStyles(j).Alignment,_
              New RectangleF(0, 0, TheGrid.GetCellBounds(i, j).Width, _
              TheGrid.GetCellBounds(i, j).Height), TheGrid(i, j).ToString())
          Next
      Next

      'init number of columns to headers
      Headers = New Header(column) {}
      SetHeaders(TheGrid, TableGrid)
      'define grid colors
      SetColors(TheGrid)

  Catch e As Exception
      Console.WriteLine(e.Message)
  End Try
End Sub

Grid Printing

Private Function PrintDataGrid(ByVal g As Graphics) As Boolean
  Dim sf As StringFormat = New StringFormat
  PageCounter = PageCounter + 1
  'if we want to print the grid right to left
  If (bRightToLeft) Then
    CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width_
               - PrintDoc.DefaultPageSettings.Margins.Right
    sf.FormatFlags = StringFormatFlags.DirectionRightToLeft
  Else
    CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
  End If

  Dim i As Integer
  For i = CurrentRow To PrintGrid.Rows - 1
    Dim j As Integer
    For j = 0 To PrintGrid.Columns - 1
      'set cell alignment
      Select Case (PrintGrid.Cell(i, j).Alignment)
        'left
        Case HorizontalAlignment.Left
          sf.Alignment = StringAlignment.Near
        Case HorizontalAlignment.Center
          sf.Alignment = StringAlignment.Center
        'right
        Case HorizontalAlignment.Right
          sf.Alignment = StringAlignment.Far
      End Select

      'advance X according to order
      If (bRightToLeft) Then
        'draw the cell bounds (lines) and back color
        g.FillRectangle(New SolidBrush(PrintGrid.BackColor), _
            CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, _
            PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height)
        g.DrawRectangle(New Pen(PrintGrid.LineColor), _
            CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, _
            PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height)

        'draw the cell text
        g.DrawString(PrintGrid.Cell(i, j).CText, _
            PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor),_
            New RectangleF(CurrentX - PrintGrid.Cell(i, j).Width, _
            CurrentY, PrintGrid.Cell(i, j).Width, _
            PrintGrid.Cell(i, j).Height), sf)

        'next cell
        CurrentX -= PrintGrid.Cell(i, j).Width
      Else
        'draw the cell bounds (lines) and back color
        g.FillRectangle(New SolidBrush(PrintGrid.BackColor), _
          CurrentX, CurrentY, PrintGrid.Cell(i, j).Width, _
          PrintGrid.Cell(i, j).Height)
        g.DrawRectangle(New Pen(PrintGrid.LineColor), CurrentX, _
          CurrentY, PrintGrid.Cell(i, j).Width, _
          PrintGrid.Cell(i, j).Height)
        'Draw text by alignment
        g.DrawString(PrintGrid.Cell(i, j).CText, _
          PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor),_
          New RectangleF(CurrentX, CurrentY, _
          PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height), sf)

        'next cell
        CurrentX += PrintGrid.Cell(i, j).Width
      End If
    Next

    'reset to beginning
    If (bRightToLeft) Then
      'right align
      CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width_
                 - PrintDoc.DefaultPageSettings.Margins.Right
    Else
      'left align
      CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
    End If

    'advance to next row
    CurrentY += PrintGrid.Cell(i, 0).Height
    CurrentRow += 1
    'if we are beyond the page margin (bottom) 
    'then we need another page,
    'return true
    If (CurrentY > PrintDoc.DefaultPageSettings.PaperSize.Height_
              - PrintDoc.DefaultPageSettings.Margins.Bottom) Then
      Return True
    End If
  Next
  Return False
End Function

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

Acknowledgements

Thanks to nashcontrol who has given me permission to upload the project converted into VB.NET.

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

Share

About the Author

Nidhi S
Web Developer
India India
Nidhi Shrivastava
B.Sc (Maths), M.Sc(Maths), PGDCA(NIIT)
Working as a freelance software developer.

Experienced in C# .NET, VB .NET, VB 6.0, SQL Server 2000, Java 2 SDK.

Worked with PMPCertify (home based) (pmpcertify.org).
Currently working with .NET and its technology for last 1 and half years. Previously worked with VB 6.0 and have done many projects using it.

Hobbies include reading, music and travelling.

You may also be interested in...

Comments and Discussions

 
QuestionNew page for right margin [modified] Pin
MichaelMCSE21-Oct-09 10:23
memberMichaelMCSE21-Oct-09 10:23 
GeneralMy vote of 2 Pin
terry nuttall2-Mar-09 5:41
memberterry nuttall2-Mar-09 5:41 
Questionhow to edit a grid data with dataset Pin
Member 334525914-Sep-08 5:15
memberMember 334525914-Sep-08 5:15 
Questionvb.net form printing problem Pin
raniranjith14-May-08 4:40
memberraniranjith14-May-08 4:40 
AnswerRe: vb.net form printing problem Pin
Nidhi S20-May-08 19:58
memberNidhi S20-May-08 19:58 
QuestionRe: vb.net form printing problem Pin
Ananda Narayanan S19-Oct-10 17:39
memberAnanda Narayanan S19-Oct-10 17:39 
QuestionNICE ARTICLE BUT ANY PROGRESS ?? Pin
gurdeeptoor23-Mar-08 9:24
membergurdeeptoor23-Mar-08 9:24 
QuestionSMS in PDU mode Pin
Nko-c30-May-07 2:15
memberNko-c30-May-07 2:15 
GeneralSearch in a data base Pin
vmarcial23-Mar-07 3:21
membervmarcial23-Mar-07 3:21 
GeneralRe: Search in a data base Pin
Nidhi S29-Mar-07 20:14
memberNidhi S29-Mar-07 20:14 
QuestionPrinting header problem Pin
makeafirstmove26-Feb-07 2:16
membermakeafirstmove26-Feb-07 2:16 
GeneralVery Useful Pin
kurienmanu@hotmail.com,10-Feb-07 1:37
memberkurienmanu@hotmail.com,10-Feb-07 1:37 
GeneralRe: Very Useful Pin
makeafirstmove26-Feb-07 0:22
membermakeafirstmove26-Feb-07 0:22 
QuestionPortrait or Landscape Pin
OumaBeskuit8-Feb-07 18:11
memberOumaBeskuit8-Feb-07 18:11 
GeneralMessage for Nidhi.s Pin
coolblim9-Jan-07 16:41
membercoolblim9-Jan-07 16:41 
Questionhow can i use vb.net code in asp.net Pin
fffffffffff8-Jan-07 2:24
memberfffffffffff8-Jan-07 2:24 
GeneralHeader for Columns missing Pin
tony joseph david12-Dec-06 3:26
membertony joseph david12-Dec-06 3:26 
GeneralRe: Header for Columns missing Pin
Nidhi S14-Dec-06 2:15
memberNidhi S14-Dec-06 2:15 
Questionchanging the view of the datagrid Pin
DBND20-Oct-06 6:53
memberDBND20-Oct-06 6:53 
Questionviewing a particular row Pin
DBND20-Oct-06 6:52
memberDBND20-Oct-06 6:52 
QuestionChange the size of the columns Pin
sax6810-Oct-06 7:08
membersax6810-Oct-06 7:08 
AnswerRe: Change the size of the columns Pin
Nidhi S10-Oct-06 18:13
memberNidhi S10-Oct-06 18:13 
QuestionRe: Change the size of the columns Pin
sax6811-Oct-06 0:20
membersax6811-Oct-06 0:20 
QuestionPrinting a grid with many columns Pin
Nko-c24-Aug-06 3:19
memberNko-c24-Aug-06 3:19 
AnswerRe: Printing a grid with many columns Pin
Nidhi S24-Aug-06 17:50
memberNidhi S24-Aug-06 17:50 
GeneralExcellent, but if dataview.rowfilter Error Pin
MXTRULA18-Jul-06 9:25
memberMXTRULA18-Jul-06 9:25 
GeneralExcellent !!!! 5 point, but one small problem [modified] Pin
MXTRULA14-Jul-06 11:51
memberMXTRULA14-Jul-06 11:51 
GeneralPrint cells of Heder Merging Pin
vakily7-Jul-06 7:34
membervakily7-Jul-06 7:34 
Generalprinting problem Pin
anil99918-Jun-06 4:20
memberanil99918-Jun-06 4:20 
GeneralPrint and Print Preview Problem Pin
clueless2322-Mar-06 22:20
memberclueless2322-Mar-06 22:20 
Generalnice supject Pin
acrossborders6-Feb-06 2:01
memberacrossborders6-Feb-06 2:01 
Questionprint preview perfect, but try to print it? Pin
rainwood4-Nov-05 3:33
memberrainwood4-Nov-05 3:33 
AnswerRe: print preview perfect, but try to print it? Pin
Duncan Edwards Jones18-Jun-06 6:51
memberDuncan Edwards Jones18-Jun-06 6:51 
Generaldelay in print previw Pin
yameen19-Oct-05 15:48
memberyameen19-Oct-05 15:48 
GeneralSub Menu datagrid Combobox Pin
rhmeen19-Aug-05 14:21
sussrhmeen19-Aug-05 14:21 
GeneralVery Helpful Pin
Mohit Bhardwaj18-Aug-05 5:52
memberMohit Bhardwaj18-Aug-05 5:52 
GeneralProblem in Preview and Printing Pin
steve chua11-Aug-05 16:50
membersteve chua11-Aug-05 16:50 
GeneralRe: Problem in Preview and Printing Pin
mina_200611-Sep-06 0:08
membermina_200611-Sep-06 0:08 
GeneralNice Article Pin
dotnet Rana12-Jun-05 18:17
memberdotnet Rana12-Jun-05 18:17 

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 | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 31 May 2005
Article Copyright 2005 by Nidhi S
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid