Results 1 to 6 of 6

Thread: Simple Printing a datagridview and richtextbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018

    Question Simple Printing a datagridview and richtextbox

    I searched on printing methods in the net , I found many comprehensive projects with different abilities.
    I need it to be as simple as possible.
    I have a datagrid view with 2 columns and 15 rows , and a richtextbox

    I tested printing a picturebox content (take datagridview image as picturebox content), quality is really poor.

    My app should have a button "print" , then it should print datagridview + the richbox , printed below datagridview.
    *the content of cells might be any size , so datagridview size should be adjusted automatically according to paper size(A4). Datagridview and richtextbox should be in center of the page

    thank you

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Simple Printing a datagridview and richtextbox

    If you search our VB.NET CodeBank forum for threads by Merrion, you will find one about a DataGridPrinter class that he created that incorporates all the functionality to use a PrintDocument and GDI+ to print a DataGridControl. Partway through that thread, you'll find a similar class dedicated to printing a DataGridView. MSDN provides a custom RichTextBox class here that incorporates its own printing functionality. They will print independently as is, so you'll need to play with them a bit to get them to print together, if it's possible, but if you want as simple as possible then those are your best bet. "Simple" is a relative term when it comes to printing heavily formatted data so don't expect a magic, one-click solution.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017

    Re: Simple Printing a datagridview and richtextbox


    JMC gave you a Tip for the DGV, here a sample for the RTB

    it is just simple Printing of the RTB, if you want to Print as formatted then it will be more

    simple printing RTB:
    'Controls: 2 Buttons; PrintDocument1 and a Richtextbox
    Imports System.Drawing.Printing
    Public Class PrintRTB '<-- my Form name
        Inherits System.Windows.Forms.Form
        Private iPageNum As Integer
        Private iOffset As Integer
        Private sData(100) As String
        Private iArraySize As Integer
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            RichTextBox1.AppendText("This is Line 1" + Environment.NewLine)
            RichTextBox1.AppendText("This is Line 2" + Environment.NewLine)
            RichTextBox1.AppendText("This is Line 3")
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End Sub
        Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, _
    ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
            'copy the Lines from RTB into an array
            iArraySize = 0
            For Each sLine As String In RichTextBox1.Lines
                sData(iArraySize) = sLine
                iArraySize += 1
        End Sub
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim iX As Single = e.MarginBounds.Left
            Dim iY As Single = e.MarginBounds.Top
            Dim aBrush As Brush = New SolidBrush(RichTextBox1.ForeColor)
            Dim iLineHeight As Single = RichTextBox1.Font.GetHeight(e.Graphics)
            iPageNum += 1
            While ((iY + iLineHeight) < e.MarginBounds.Bottom And iOffset < iArraySize)
                e.Graphics.DrawString(sData(iOffset), RichTextBox1.Font, aBrush, iX, iY)
                iOffset += 1
                iY += iLineHeight
            End While
            If iOffset < iArraySize Then
                e.HasMorePages = True
            End If
        End Sub
    End Class
    for advanced Printing
    EDIT: deleted the Link, just saw that JMC provided that Link

    Last edited by ChrisE; Feb 26th, 2018 at 02:50 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2018

    Re: Simple Printing a datagridview and richtextbox

    thank you guys , I go for it asap

  5. #5
    Junior Member
    Join Date
    Nov 2017

    Re: Simple Printing a datagridview and richtextbox

    Don't use that picturebox thing because some of the rows from datagridview is not displayed in the image. Instead use a conversion function like converting to PDF then print. Create a two text box which is titletext.text which is the header of the printed document and locationtxt.text serves as a location of the file like this (Cocuments/Desktop/Printeddocx.pdf) there should be a blank pdf created. But first You must add to the reference the iTextSharp.dll research for it how to add in the reference google it.Here is the code where you can play with :

     Private Sub Printbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Printbtn.Click
            Dim dtaset As New DataTable
            If titletext.Text = "" Then
                MessageBox.Show("Input the Title of the Record!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            ElseIf locationtxt.Text = "" Then
                MessageBox.Show("Choose PDF File to Save!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            ElseIf DataGridView1.RowCount <= 1 Then
                MsgBox("There are no displayed data, Please Choose Users Data First.")
            ElseIf MsgBox(" Are you sure you want to export to PDF then print the data? ", MsgBoxStyle.Question + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, "Confirmation...") = MsgBoxResult.Yes Then
                Dim Paragraph As New Paragraph ' declaration for new paragraph
                Dim PdfFile As New Document(PageSize.A4, 40, 40, 40, 20) ' set pdf page size
                PdfFile.AddTitle(titletext.Text) ' set our pdf title
                Dim Write As PdfWriter = PdfWriter.GetInstance(PdfFile, New FileStream(locationtxt.Text, FileMode.Create))
                ' declaration font type
                Dim pTitle As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
                Dim pTable As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
                ' insert title into pdf file
                Paragraph = New Paragraph(New Chunk(titletext.Text, pTitle))
                Paragraph.Alignment = Element.ALIGN_CENTER
                Paragraph.SpacingAfter = 5.0F
                ' set and add page with current settings
                ' create data into table
                Dim PdfTable As New PdfPTable(DataGridView1.Columns.Count)
                ' setting width of table
                PdfTable.TotalWidth = 500.0F
                PdfTable.LockedWidth = True
                Dim widths(0 To DataGridView1.Columns.Count - 1) As Single
                For i As Integer = 0 To DataGridView1.Columns.Count - 1
                    widths(i) = 1.0F
                PdfTable.HorizontalAlignment = 0
                PdfTable.SpacingBefore = 5.0F
                ' declaration pdf cells
                Dim pdfcell As PdfPCell = New PdfPCell
                ' create pdf header
                For i As Integer = 0 To DataGridView1.Columns.Count - 1
                    pdfcell = New PdfPCell(New Phrase(New Chunk(DataGridView1.Columns(i).HeaderText, pTable)))
                    ' alignment header table
                    pdfcell.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                    ' add cells into pdf table
                ' add data into pdf table
                For i As Integer = 0 To DataGridView1.Rows.Count - 2
                    For j As Integer = 0 To DataGridView1.Columns.Count - 1
                        pdfcell = New PdfPCell(New Phrase(DataGridView1(j, i).Value.ToString(), pTable))
                        PdfTable.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                ' add pdf table into pdf document
                PdfFile.Close() ' close all sessions
                MessageBox.Show("PDF format success exported !", "Informations", MessageBoxButtons.OK, MessageBoxIcon.Information)
                titletext.Text = ""
                MessageBox.Show("The PDF exported is located in (Your location) folder")
            End If
        End Sub

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Chelmsford UK

    Re: Simple Printing a datagridview and richtextbox

    For printing a multi-page or single page dgv, see...


    For printing a RTB with WYSIWYG printing, see...


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width