שתף באמצעות


vb.net need a code to print my datagridview also some textbox values before and after the datagridview also in thermal printer using vb.net

Question

Wednesday, December 9, 2015 8:50 AM

Hi i am new to vb.net need your help 

All replies (1)

Wednesday, December 9, 2015 1:01 PM ✅Answered | 1 vote

I am not sure what you want with the textboxes but this sample will print a dgv.  The sample prints using high resolution graphics.

You need to allow the user to select the printer they want using the PrintDialog control or you need to set the PrintDocument1.PrinterSettings to the printer you want with code.

Option Strict On
Imports System.Drawing.Printing

Public Class Form1
    'print dgv using graphics
    '(c) 2015 Sandia Software All Rights Reserved
    Private WithEvents dgv As New DataGridView
    Private WithEvents PrintDocument1 As PrintDocument = New PrintDocument
    Private PrintPreviewDialog1 As New PrintPreviewDialog
    Private PagesPrinted, LInesPrinted As Integer
    Private PrintingActive As Boolean

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'create the dgv and data
        Me.Controls.Add(dgv)
        dgv.Dock = DockStyle.Fill
        dgv.RowTemplate.Height = 50
        dgv.RowTemplate.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft
        dgv.ColumnCount = 3
        dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgv.Columns(0).Name = "Product ID"
        dgv.Columns(1).Name = "Product Name"
        dgv.Columns(2).Name = "Product Description"
        dgv.Columns(2).Width = 300

        For r = 0 To 30
            dgv.Rows.Add({"ID " & r.ToString, "Product " & r.ToString, "The product is: " & r.ToString})
        Next

        'show the Print Preview Dialog
        'click the print button on the print preview dialog to print
        PrintingActive = False
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()

    End Sub    Private Function GetDGVHeaderTextOffset(theDGV As DataGridView, c As Integer) As Point
        Select Case theDGV.Columns(0).HeaderCell.Style.Alignment
            Case DataGridViewContentAlignment.MiddleCenter

        End Select
    End Function    Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
        SetPrintOrigin(e)

        Static LastRow As Integer = 0
        Dim CellTopPos As Integer = e.MarginBounds.Top
        Dim CellHeight As Integer = dgv.Columns(0).HeaderCell.Size.Height
        Dim offset As Integer = CInt(CellHeight / 10)

        PagesPrinted += 1

        Using f As Font = New Font(dgv.Font.FontFamily, dgv.Font.Size)
            Using br As New SolidBrush(Color.Black)
                'draw the header
                Dim CellLeftPos As Integer = e.MarginBounds.Left
                For col = 0 To dgv.ColumnCount - 1
                    Dim CellValue As String = dgv.Columns(col).Name
                    Dim CellWidth = dgv.Columns(col).Width + 50

                    e.Graphics.FillRectangle(Brushes.LightGray, CellLeftPos, CellTopPos, CellWidth, CellHeight)
                    e.Graphics.DrawString(CellValue, f, br, CellLeftPos + offset, CellTopPos + offset)
                    e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)

                    CellLeftPos += CellWidth
                Next
                CellTopPos += CellHeight

                'draw the rows
                For Row = LastRow To dgv.RowCount - 2
                    CellLeftPos = e.MarginBounds.Left
                    CellHeight = dgv.Rows(0).Height

                    For Cell = 0 To dgv.ColumnCount - 1
                        Dim CellValue As String = dgv.Rows(Row).Cells(Cell).Value.ToString()
                        Dim CellWidth = dgv.Rows(Row).Cells(Cell).Size.Width + 50

                        e.Graphics.DrawString(CellValue, f, br, CellLeftPos + offset, CellTopPos + offset)
                        e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)

                        CellLeftPos += CellWidth
                    Next

                    CellTopPos += dgv.Rows(Row).Cells(0).Size.Height

                    If CellTopPos > e.MarginBounds.Bottom - dgv.Rows(Row).Cells(0).Size.Height Then
                        e.HasMorePages = True
                        LastRow = Row + 1
                        Exit Sub
                    End If
                Next
            End Using
        End Using

        e.HasMorePages = False
        PagesPrinted = 0
        LastRow = 0

    End Sub

    Private Sub SetPrintOrigin(ByRef e As PrintPageEventArgs)
        'for print preview the picturebox graphics coord 0,0 is at upper left of the white page graphic
        'but for the printer 0,0 is at the non printable offset from the edge of the paper
        'so we need to translate 0, 0 to draw to the printer but not the preview picturebox
        'the first call here is for the preview graphic, then all calls after are to print
        If PrintingActive Then
            Dim paRectF As RectangleF
            paRectF = PrintDocument1.DefaultPageSettings.PrintableArea()
            e.Graphics.TranslateTransform(-paRectF.X, -paRectF.Y)
        Else
            PrintingActive = True
        End If
    End Sub
End Class