Thursday, June 27, 2013

Printing DataGridView's on a sheet in VB

This one is for you VB programmers out there that need a little help with the DataGridView printing.  Lately I have been writing a lot of selection and list programs, and one that I have written was for engineers to list out valves, control valves, and equipment for projects to be able to send them out for quoting and design purposes.  One issue that I ran into was being able to get the DataGridView to print each line that is entered into the grid.  But, I was able to find a bit of code and with a few modifications of my own...I was able to get it to print all lines entered into the grid, and also allow the programmer to change location of the grid on the printdocument file.
So, below is the code for the datagridview:

Dim mRow As Integer = 0
Dim newpage As Boolean = True
Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim tmpSize As New SizeF()
Dim tmpFont As Font
For i As Integer = 0 To DataGridView1.Columns.Count()
tmpFont = DataGridView1.ColumnHeadersDefaultCellStyle.Font
If tmpFont Is Nothing Then
tmpFont = DataGridView1.DefaultCellStyle.Font
End If
For j As Integer = 0 To DataGridView1.Rows.Count - 1
tmpFont = DataGridView1.Rows(j).DefaultCellStyle.Font
If tmpFont Is Nothing Then
tmpFont = DataGridView1.DefaultCellStyle.Font
End If
Dim cellsPerRow As New List(Of Integer)
Dim rowHeight As Integer = DataGridView1.ColumnHeadersHeight + DataGridView1.Rows(0).Height
Dim cellWidths(DataGridView1.Columns.Count - 1) As Integer
Dim rowWidths As New List(Of Integer)
Dim cellCounter As Integer = 0
With DataGridView1
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment =
StringAlignment.Center fmt.Trimming =
StringTrimming.EllipsisCharacter Dim y As Single = e.MarginBounds.Top + 120
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If (newpage) Then
e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font,
Brushes.Black, rc, fmt)
e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
End If
x += rc.Width
h =
Math.Max(h, rc.Height) Next
newpage =
False y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages =
True mRow -= 1
newpage =
True Exit Sub
End If
mRow = 0
End With
End Sub

So, the bit of code that is highlighted in yellow is where you can control the vertical location of the grid on the printdocument.  Also if you want to change the location of the grid horizontally then; three lines below "dim y" there is the code line for the "dim x as single" you can change the value of that to change the horizontal location of the grid.

Happy Coding!


  1. If you are looking to print the correct lineweights you have applied to your drawings there is a methods you can follow.

    The first is to make sure the LWT (line weight) button is turned on below your command bar at the base of the window, this will allow you to view line thickness in the created drawing before going to your plot options.
    CAD printing

  2. Hey Serina,
    this post was actually for being able to print a datagridview from a visual basic form; not printing in AutoCAD. Thank you for the comment though.