Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been looking for code that import and print DataGridView and I found it. My purpose is to make a billing system for law firm. I've printed the name of the lawyer and the adresse and etc and after that, I want to print the datagridview of the bill but the problem is that, vb print it over the info of lawyer. my purpose is to print the gatagridview under the lawyer info . there was a solution on the internet like:
e.Graphics.DrawRectangle(blackPen, New Rectangle(position, yPosition, totalWidth,                    height))
I have tried hard the implement it in the code but it didn't work Please help
here is the code:
 Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    'lawyer info
    e.Graphics.DrawString("Facture", New Font("Arial", 24, FontStyle.Italic), Brushes.Blue, New Point(200, 20))
    e.Graphics.DrawString("Nom Avocat : " + TextBox17.Text, New Font("Arial", 12, FontStyle.Italic), Brushes.Black, New Point(20, 60))
    e.Graphics.DrawString("Adresse : " + TextBox18.Text, New Font("Arial", 12, FontStyle.Italic), Brushes.Black, New Point(20, 80))
    e.Graphics.DrawString("Ville : " + TextBox19.Text, New Font("Arial", 12, FontStyle.Italic), Brushes.Black, New Point(20, 100))
    e.Graphics.DrawString("Telephone : " + TextBox20.Text, New Font("Arial", 12, FontStyle.Italic), Brushes.Black, New Point(20, 120))
    e.Graphics.DrawString("Email : " + TextBox21.Text, New Font("Arial", 12, FontStyle.Italic), Brushes.Black, New Point(20, 140))
    e.Graphics.DrawString("Site web : " + TextBox22.Text, New Font("Arial", 12, FontStyle.Italic), Brushes.Black, New Point(20, 160))
    e.Graphics.DrawString("Site web : " + TextBox22.Text, New Font("Arial", 12, FontStyle.Italic), Brushes.Black, New Point(20, 180))

    'positioning of datagriview2




    'insert datagridview
    ' sets it to show '...' for long text
    Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
    fmt.LineAlignment = StringAlignment.Center
    fmt.Trimming = StringTrimming.EllipsisCharacter
    Dim y As Int32 = e.MarginBounds.Top
    Dim rc As Rectangle
    Dim x As Int32
    Dim h As Int32 = 0
    Dim row As DataGridViewRow

    ' print the header text for a new page
    '   use a grey bg just like the control
    If newpage Then
        row = DataGridView2.Rows(mRow)
        x = e.MarginBounds.Left
        For Each cell As DataGridViewCell In row.Cells
            ' since we are printing the control's view,
            ' skip invidible columns
            If cell.Visible Then
                rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)

                e.Graphics.FillRectangle(Brushes.LightGray, rc)
                e.Graphics.DrawRectangle(Pens.Black, rc)

                ' reused in the data pront - should be a function
                Select Case DataGridView2.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
                    Case DataGridViewContentAlignment.BottomRight,
                         DataGridViewContentAlignment.MiddleRight
                        fmt.Alignment = StringAlignment.Far
                        rc.Offset(-1, 0)
                    Case DataGridViewContentAlignment.BottomCenter,
                        DataGridViewContentAlignment.MiddleCenter
                        fmt.Alignment = StringAlignment.Center
                    Case Else
                        fmt.Alignment = StringAlignment.Near
                        rc.Offset(2, 0)
                End Select

                e.Graphics.DrawString(DataGridView2.Columns(cell.ColumnIndex).HeaderText,
                                           DataGridView2.Font, Brushes.Black, rc, fmt)
                x += rc.Width
                h = Math.Max(h, rc.Height)
            End If
        Next
        y += h

    End If
    newpage = False

    ' now print the data for each row
    Dim thisNDX As Int32
    For thisNDX = mRow To DataGridView2.RowCount - 1
        ' no need to try to print the new row
        If DataGridView2.Rows(thisNDX).IsNewRow Then Exit For

        row = DataGridView2.Rows(thisNDX)
        x = e.MarginBounds.Left
        h = 0

        ' reset X for data
        x = e.MarginBounds.Left

        ' print the data
        For Each cell As DataGridViewCell In row.Cells
            If cell.Visible Then
                rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)

                ' SAMPLE CODE: How To 
                ' up a RowPrePaint rule
                'If Convert.ToDecimal(row.Cells(5).Value) < 9.99 Then
                '    Using br As New SolidBrush(Color.MistyRose)
                '        e.Graphics.FillRectangle(br, rc)
                '    End Using
                'End If

                e.Graphics.DrawRectangle(Pens.Black, rc)

                Select Case DataGridView2.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
                    Case DataGridViewContentAlignment.BottomRight,
                         DataGridViewContentAlignment.MiddleRight
                        fmt.Alignment = StringAlignment.Far
                        rc.Offset(-1, 0)
                    Case DataGridViewContentAlignment.BottomCenter,
                        DataGridViewContentAlignment.MiddleCenter
                        fmt.Alignment = StringAlignment.Center
                    Case Else
                        fmt.Alignment = StringAlignment.Near
                        rc.Offset(2, 0)
                End Select

                e.Graphics.DrawString(cell.FormattedValue.ToString(),
                                     DataGridView2.Font, Brushes.Black, rc, fmt)

                x += rc.Width
                h = Math.Max(h, rc.Height)
            End If

        Next
        y += h
        ' next row to print
        mRow = thisNDX + 1

        If y + h > e.MarginBounds.Bottom Then
            e.HasMorePages = True
            ' mRow -= 1   causes last row to rePrint on next page
            newpage = True
            Return
        End If
    Next
    ' end instert



End Sub


What I have tried:

e.Graphics.DrawRectangle(blackPen, New Rectangle(position, yPosition, totalWidth,                    height))
Posted
Updated 31-Jul-20 7:10am
Comments
Richard MacCutchan 31-Jul-20 3:54am    
Don't use fixed value for the position that you are printing at. Start with the top left position of the page for the first item, and calculate the position of each following item by the size and position of its predecessor.

1 solution

You should compose your entire invoice as a "Form", and print that. Same with any other form requiring images, borders, etc. Then print the entire form. Composing becomes a lot simpler. Works for visuals in WPF too using a different technique.

How to: Print a Form by Using the PrintForm Component (Visual Basic) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900