print datagrid

Status
Niet open voor verdere reacties.

old Hippy

Gebruiker
Lid geworden
24 mei 2008
Berichten
911
Hallo allemaal

ik ben bezig met het maken van een print module deze is nu werkend.
maar wat ik nog graag zou willen!!

Dat de Header text aangepast word voor de verschillende Datagrids die ik uit print.

B.v. Datadridview1 in form1 Heefd hier voor de volgende code
Code:
 ' Draw Header
                    e.Graphics.DrawString("[COLOR="Red"]Overzicht " + Form1.maand.text + Form1 Jaar.text[/COLOR], New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)

Dit werkt goed maar mijn programma heeft vier datagridview's op verschillende forms.
Form1, form2, ezv.
Hoe zorg ik er voor dat de code het juiste form en textboxen ziet.????

Old Hippy
 

Bijlagen

  • Knipsel10.JPG
    Knipsel10.JPG
    23,1 KB · Weergaven: 25
Laatst bewerkt:
Hallo evertVB

Bedankt voor de reactie.
maar hoe doe ik dit ik heb geen idee.
werk voor het eerst met een module.
 
Ik dacht dat je nu wel zoiets zou hebben:
Code:
    Private Sub OldHippyPrintModule()
      ...
    End Sub

En dat wordt dan nu:

Code:
    Private Sub OldHippyPrintModule(ByVal MyHeader As String)
      ...
      (en doe iets met MyHeader)
      ...
    End Sub
 
sorry evertVB
dit is het gedeelte voor het printen.
heb geen idee hoe jouw code te gebruiken?

Dit is niet de gehele module




Code:
 Public Sub DocToPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles DocToPrint.PrintPage
        [COLOR="Red"]Header = "Overzicht: [/COLOR]"
        Static oColumnLefts As New ArrayList
        Static oColumnWidths As New ArrayList
        Static oColumnTypes As New ArrayList
        Static nHeight As Int16
        Dim nWidth, i, nRowsPerPage As Int16
        Dim nTop As Int16 = e.MarginBounds.Top
        Dim nLeft As Int16 = e.MarginBounds.Left

        If nPageNo = 1 Then
            oColumnLefts.Clear()
            oColumnWidths.Clear()
            oColumnTypes.Clear()
            For Each oColumn As DataGridViewColumn In DataGridView1.Columns
                If oColumn.Visible = True Then ' Prints cols with Visible property
                    nWidth = CType(Math.Floor(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)
                    nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 11
                    oColumnLefts.Add(nLeft)
                    oColumnWidths.Add(nWidth)
                    oColumnTypes.Add(oColumn.GetType)
                    nLeft += nWidth
                End If
            Next
        End If

        Do While nRowPos < DataGridView1.Rows.Count - 1 ' Reports of failing to Print last 
            'row. If so remove -1 from above line
            Dim oRow As DataGridViewRow = DataGridView1.Rows(nRowPos)
            If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
                DrawFooter(e, nRowsPerPage)
                NewPage = True
                nPageNo += 1
                e.HasMorePages = True
                Exit Sub
            Else
                If NewPage Then
                    ' Draw Header
                    Dim Formnaam As New Form
                    e.Graphics.DrawString([COLOR="Red"]Header[/COLOR], New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
                    ' Draw Columns
                    nTop = e.MarginBounds.Top
                    i = 0
                    For Each oColumn As DataGridViewColumn In DataGridView1.Columns
                        If oColumn.Visible = True Then ' Prints cols with Visible property
                            e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
                            e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
                            e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
                            i += 1
                        End If
                    Next
                    NewPage = False
                End If
                nTop += nHeight
                i = 0
                Dim svalue As String
                For Each oCell As DataGridViewCell In oRow.Cells
                    If oCell.Visible = True Then ' Prints cols with Visible property
                        If oCell.Value IsNot Nothing Then
                            svalue = oCell.Value.ToString
                        Else
                            svalue = ""
                        End If
                        If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
                            e.Graphics.DrawString(svalue, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
                        ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
                            oButton.Text = oCell.Value.ToString
                            oButton.Size = New Size(oColumnWidths(i), nHeight)
                            Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
                            oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, _
                            oBitmap.Height))
                            e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
                        ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
                            oCheckbox.Size = New Size(14, 14)
                            oCheckbox.Checked = CType(oCell.Value, Boolean)
                            Dim oBitmap As New Bitmap(oColumnWidths(i), nHeight)
                            Dim oTempGraphics As Graphics = Graphics.FromImage(oBitmap)
                            oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0, _
                            0, oBitmap.Width, oBitmap.Height))
                            oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width - oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) / 2, Int32), oCheckbox.Width, _
                            oCheckbox.Height))
                            e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
                        ElseIf oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
                            oComboBox.Size = New Size(oColumnWidths(i), nHeight)
                            Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
                            oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, _
                            oBitmap.Height))
                            e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
                            e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, _
                            New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) _
                            - 16, nHeight), oStringFormatComboBox)
                        ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn) Then
                            Dim oCellSize As Rectangle = New Rectangle(oColumnLefts(i), nTop, _
                            oColumnWidths(i), nHeight)
                            Dim oImageSize As Size = CType(oCell.Value, Image).Size
                            e.Graphics.DrawImage(oCell.Value, New Rectangle(oColumnLefts(i) + CType(((oCellSize.Width - oImageSize.Width) / 2), Int32), nTop + CType(((oCellSize.Height - oImageSize.Height) / 2), Int32), _
                            CType(oCell.Value, Image).Width, CType(oCell.Value,  _
                            Image).Height))

                        End If
                        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), _
                        nTop, oColumnWidths(i), nHeight))

                        i += 1
                    End If
                Next

            End If
            nRowPos += 1
            nRowsPerPage += 1
        Loop
        DrawFooter(e, nRowsPerPage)
        e.HasMorePages = False

    End Sub


Het zelfde probleem heb ik hier bij de Footer
Waar op het form oderstotaal textbox3 word uitgelezen
maar bij het form Artikeldetail moet het textbox10 uit lezen.
is dit van uit het form Artikeldetail of oderstotaal te regelen?????

Code:
 Public Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
        Dim sPageNo As String = nPageNo.ToString + " of "
        If nPageNo = "1" Then
            lPageNo = Math.Ceiling((DataGridView1.Rows.Count - 1) / RowsPerPage).ToString()
            sPageNo = nPageNo.ToString + " of " + lPageNo
        Else
            sPageNo = nPageNo.ToString + " of " + lPageNo
        End If
        ' Right Align - Totaal
        e.Graphics.DrawString[COLOR="Red"]("Bruto winst Excl: " + Orderstotaal.TextBox3.Text[/COLOR], New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, _
        e.MarginBounds.Left - 120 + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, _
        e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 4)
        ' Left Align - Date/Time
        e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, _
       New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, _
        e.MarginBounds.Top + e.MarginBounds.Height + 4) '7 is a mod (was 31)to raise page no. up one line
        ' Center - Page No. Info
        e.Graphics.DrawString(sPageNo, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, _
       New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Width) / 2, _
        e.MarginBounds.Top + e.MarginBounds.Height + 4)

    End Sub
 
Breid dan DrawFooter uit met een parameter.
heb geen idee hoe jouw code te gebruiken?
Je hoeft mijn code niet gebruiken.
Ik doe alleen maar de suggestie om een parameter te gebruiken.

Of snap ik totaal niet wat jouw vraag is?
 
Beste evertVB

Ik denk dat je mij goed begrijpt.

maar ik jou niet kan je mij een voorbeeld geven.
voor de parameter in de DrawFooter.

sorry ik ben maar een Hobby programmer.:o
 
Dit is wat je nu hebt:
Code:
    Public Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
        Dim sPageNo As String = nPageNo.ToString + " of "
        If nPageNo = "1" Then
            lPageNo = Math.Ceiling((DataGridView1.Rows.Count - 1) / RowsPerPage).ToString()
            sPageNo = nPageNo.ToString + " of " + lPageNo
        Else
            sPageNo = nPageNo.ToString + " of " + lPageNo
        End If
        ' Right Align - Totaal
        e.Graphics.DrawString("Bruto winst Excl: " + Orderstotaal.TextBox3.Text, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, _
        e.MarginBounds.Left - 120 + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, _
        e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 4)
        ' Left Align - Date/Time
        e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, _
       New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, _
        e.MarginBounds.Top + e.MarginBounds.Height + 4) '7 is a mod (was 31)to raise page no. up one line
        ' Center - Page No. Info
        e.Graphics.DrawString(sPageNo, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, _
       New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Width) / 2, _
        e.MarginBounds.Top + e.MarginBounds.Height + 4)

    End Sub

Dit is hoe je een parameter toevoegt:

Code:
    Public Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32, ByVal MyHeader As String)
        Dim sPageNo As String = nPageNo.ToString + " of "
        If nPageNo = "1" Then
            lPageNo = Math.Ceiling((DataGridView1.Rows.Count - 1) / RowsPerPage).ToString()
            sPageNo = nPageNo.ToString + " of " + lPageNo
        Else
            sPageNo = nPageNo.ToString + " of " + lPageNo
        End If
        ' Right Align - Totaal
        e.Graphics.DrawString(MyHeader, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, _
        e.MarginBounds.Left - 120 + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, _
        e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 4)
        ' Left Align - Date/Time
        e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, _
       New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, _
        e.MarginBounds.Top + e.MarginBounds.Height + 4) '7 is a mod (was 31)to raise page no. up one line
        ' Center - Page No. Info
        e.Graphics.DrawString(sPageNo, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, _
       New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Width) / 2, _
        e.MarginBounds.Top + e.MarginBounds.Height + 4)

    End Sub

En op de plek waar je DrawFooter aanroept:

Code:
DrawFooter(...,...)
wordt
Code:
DrawFooter(...,...,"Bruto winst Excl: " + Orderstotaal.TextBox3.Text)

(Ik heb er puntjes gezet omdat ik natuurlijk niet weet hoe jouw code er nu op die plek uit ziet.)
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan