none
best way to print in vb.net RRS feed

All replies

  • Hi,

    The following method can print DataGridview.

            Dim bitmap As Bitmap
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim height As Integer = DataGridView1.Height
            DataGridView1.Height = DataGridView1.RowCount * DataGridView1.RowTemplate.Height
            bitmap = New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)
            DataGridView1.DrawToBitmap(bitmap, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))
            DataGridView1.Height = height
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            e.Graphics.DrawImage(bitmap, 0, 0)
        End Sub
    
    

    see the following link:

    https://stackoverflow.com/questions/5398354/best-way-to-print-a-datagridview-with-all-rows-and-all-columns

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 10, 2019 9:28 AM
  • thanks for reply

    i have some idea how to print

    but i need help not in code just idea

    i use datagridview and textbox and some label and datetimepicker

    i will print it ,i am think of print directly a form or  what i can use for that

    please help

    Friday, May 10, 2019 9:47 AM
  • If by chance your DataGridView has a DataSource such as a DataTable you can cast the DataGridView.DataSource to a DataTable 

    Dim dt = CType(DataGridView1.DataSource,DataTable)

    Then send it to this function where each <td> is a DataColumn in the DataTable which creates a HTML structure (you can even style it if you like but not getting into this), the parameter textBoxValue is the value of your TextBox. If not using a DataTable then you would send a DataGridView to the method below and alter it to accept a DataGridView where dt.AsEumberable changes to iterate DataGridViewRows and the <td> needs changing to work with cells.

    Private Function DataTableToXml(
                               ByVal dt As DataTable,
                               ByVal textBoxValue As String
                               ) As String
    
        Dim document =
                <html>
                    <Body>
                        <label><%= textBoxValue %></label>
                        <Table>
                            <%= From T In dt.AsEnumerable
                                Select
                        <tr>
                            <td><%= T.Field(Of String)("SomeColumnName1") %></td>
                            <td><%= T.Field(Of String)("SomeColumnName2") %></td>
                            <td><%= T.Field(Of Integer)("SomeColumnName3") %></td>
                        </tr> %>
                        </Table>
                    </Body>
                </html>
    
        Return document.ToString()
    
    End Function

    Once you have the string back use the following to print.

    https://stackoverflow.com/questions/4168117/printing-with-vb-net


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 10, 2019 10:29 AM
    Moderator
  • thanks for reply

    i have some idea how to print

    but i need help not in code just idea

    i use datagridview and textbox and some label and datetimepicker

    i will print it ,i am think of print directly a form or  what i can use for that

    please help

    Hi Houssem12,

    I don't know if this code can help you or not but give it a try

    Private PrescriptionStr As String
    
    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    
            PrescriptionStr = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine &
                                "          " & "Name  : " & txtVisName.Text & vbCrLf &
                                "          " & lblcurTime.Text & "          " & "Visit No.: " & txtVisNo.Text & "          " & "Patient ID : " & txtVisPatNo.Text & vbCrLf &        '"    " & "R/: " & vbNewLine &
                                "          " & "Diagnosis : " & cbxDia.Text & vbCrLf & vbCrLf & vbCrLf &
                                "                    " & cbxDrug1.Text & vbNewLine & "                                                   " & cbxPlan1.Text & vbCrLf & vbCrLf & vbCrLf &
                                "                    " & cbxDrug2.Text & vbCrLf & "                                                  " & cbxPlan2.Text & vbCrLf & vbCrLf &
                                "                    " & cbxDrug3.Text & vbCrLf & "                                                  " & cbxPlan3.Text & vbCrLf & vbCrLf &
                                "                    " & cbxDrug4.Text & vbCrLf & "                                                  " & cbxPlan4.Text & vbCrLf & vbCrLf &
                                "                    " & cbxDrug5.Text & vbCrLf & "                                                  " & cbxPlan5.Text & vbCrLf & vbCrLf &
                                "                    " & cbxDrug6.Text & vbCrLf & "                                                  " & cbxPlan6.Text & vbCrLf & vbCrLf &
                                "                    " & cbxDrug7.Text & vbCrLf & "                                                  " & cbxPlan7.Text & vbCrLf & vbCrLf &
                                "                    " & cbxDrug8.Text & vbCrLf & "                                                  " & cbxPlan8.Text & vbCrLf &
                                "                    " & cbxDrug9.Text & vbCrLf & "                                                  " & cbxPlan9.Text & vbCrLf & vbCrLf & vbCrLf & vbCrLf
    
    
            If cbxDrug1.Text <> "" And txtVisName.Text <> "" Then
                PrintDocument1.Print()
            End If
    
        End Sub
    
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
     Dim numChars As Integer
     Dim numLines As Integer
     Dim stringForPage As String
     Dim strFormat As New StringFormat()
     Dim PrintFont As Font
    
     PrintFont = Label6.Font 
     PrintFont = Label12.Font
     PrintFont = txtVisName.Font 
     PrintFont = txtVisPatNo.Font  
     PrintFont = cbxDia.Font
     PrintFont = lblcurTime.Font 
     PrintFont = txtVisNo.Font
     PrintFont = cbxDrug1.Font 
     PrintFont = cbxPlan1.Font 
     PrintFont = cbxDrug2.Font 
     PrintFont = cbxPlan2.Font
     PrintFont = cbxDrug3.Font 
     PrintFont = cbxDrug4.Font 
     PrintFont = cbxPlan4.Font 
     PrintFont = cbxDrug5.Font
     PrintFont = cbxPlan5.Font 
     PrintFont = cbxDrug6.Font 
     PrintFont = cbxPlan6.Font 
     PrintFont = cbxDrug7.Font
     PrintFont = cbxPlan7.Font 
     PrintFont = cbxDrug8.Font 
     PrintFont = cbxPlan8.Font  
    
    
     Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
            Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
            strFormat.Trimming = StringTrimming.Word
            e.Graphics.MeasureString(PrescriptionStr, PrintFont, sizeMeasure, strFormat, numChars, numLines)
            stringForPage = PrescriptionStr.Substring(0, numChars)
            e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
    
            If numChars < PrescriptionStr.Length Then
                PrescriptionStr = PrescriptionStr.Substring(numChars)
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
    
    
        End Sub
    

    This code print a doctor prescription .... It's so useful to print labels ,textboxes and comboboxes i don't know if it can print datagridview or not but you can use This link to print Datagridview

    I hope it helps to solve your issue .....


    Regards From Egypt

    Friday, May 10, 2019 12:34 PM
  • thank you for helping and i have another try

    i use a printform

     PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
            PrintForm1.Print()

    it work fine but i want the form not opened just a report where to print


    and resize the page
    • Edited by Houssem12 Monday, May 13, 2019 12:01 PM
    Monday, May 13, 2019 11:41 AM
  • Hi,

    Have you solved this issue?

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/5cdc8bf6-5ded-495a-a619-cc082c6e02b0/i-am-very-confused-in-printing-in-vbnet?forum=vbgeneral

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 17, 2019 7:58 AM