UML Design
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
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
row = TableGrid.Rows.Count
If (TheGrid.TableStyles.Count = 0) Then
CreateColumnStyles(TheGrid, TableGrid)
Else
If (TheGrid.TableStyles(TableGrid.TableName).GridColumnStyles.Count=0) Then
CreateColumnStyles(TheGrid, TableGrid
End If
End If
column = TheGrid.TableStyles(TableGrid.TableName).GridColumnStyles.Count
Cells = New Cell(Rows, Columns) {}
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
Headers = New Header(column) {}
SetHeaders(TheGrid, TableGrid)
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 (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
Select Case (PrintGrid.Cell(i, j).Alignment)
Case HorizontalAlignment.Left
sf.Alignment = StringAlignment.Near
Case HorizontalAlignment.Center
sf.Alignment = StringAlignment.Center
Case HorizontalAlignment.Right
sf.Alignment = StringAlignment.Far
End Select
If (bRightToLeft) Then
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)
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)
CurrentX -= PrintGrid.Cell(i, j).Width
Else
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)
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)
CurrentX += PrintGrid.Cell(i, j).Width
End If
Next
If (bRightToLeft) Then
CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width_
- PrintDoc.DefaultPageSettings.Margins.Right
Else
CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
End If
CurrentY += PrintGrid.Cell(i, 0).Height
CurrentRow += 1
If (CurrentY > PrintDoc.DefaultPageSettings.PaperSize.Height_
- PrintDoc.DefaultPageSettings.Margins.Bottom) Then
Return True
End If
Next
Return False
End Function
Current Todo
- Fix - When grid is empty, header is not printed well.
- When grid is larger 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.