datagridview printen

  • Onderwerp starter Onderwerp starter leon1
  • Startdatum Startdatum
Status
Niet open voor verdere reacties.

leon1

Gebruiker
Lid geworden
10 feb 2010
Berichten
10
hallo,

ik heb een klein programmatje geschreven met visual basic.
ik heb er een datagridview in gemaakt met comboboxen.
en nu wil ik dat ik die datagridview kan uitprinten in een tabel.

kan iemand mij hier mee helpen ?

groetjes leon
 
Hi hopelijk bedoel je dit
Wel je dataGridview maam aanpassen
Deze Code in je form
Code:
 Private Sub PrintToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripButton.Click
             PrintGrid.StartPrintL(CataloogDataGridView) 'CallPrint module Sub
    End Sub
maak een print modulle
Code:
Imports System.Drawing.Printing
Module PrintGrid
    Public oPortrait As Boolean
    Public WithEvents PrintDocument1 As New PrintDocument 'Added by me to overcome WithEvents required error
    Public DataGridView1 As New DataGridView 'Added by me to overcome DataGridView1 not declared error
    Public DefaultPageSettings As PageSettings = New PageSettings()
    Private WithEvents PrintPreviewDialog1 As New PrintPreviewDialog
    Public WithEvents DocToPrint As New PrintDocument 'tt6
    Dim oLandscape As Boolean
    Dim GridToPrint As DataGridView
    Dim PageSetUpDialog1 As New PageSetupDialog 'trial 1
    Private lPageNo As String = ""
    Private sPageNo As String = ""
    Private oStringFormat As StringFormat
    Private oStringFormatComboBox As StringFormat
    Private oButton As Button
    Private oCheckbox As CheckBox
    Private oComboBox As ComboBox
    Private nTotalWidth As Int16
    Private nRowPos As Int16
    Private NewPage As Boolean
    Private nPageNo As Int16
    Private Header As String
    Private sUserName As String = " Old Hippy"
    Private mRow As Integer


    Public Sub StartPrintL(ByVal GridToPrint) 'ByVal GridToPrint As DataGridView)
        DataGridView1 = GridToPrint
        DocToPrint = PrintDocument1
        oLandscape = True 'used in previous attempt
        ' Set up Default Page Settings
        PrintDocument1.DefaultPageSettings.Landscape = True
        PrintDocument1.DefaultPageSettings.Margins.Left = 100
        PrintDocument1.DefaultPageSettings.Margins.Right = 100
        PrintDocument1.DefaultPageSettings.Margins.Top = 100
        PrintDocument1.DefaultPageSettings.Margins.Bottom = 100
        DataGridView1.Columns(2).Visible = False ' Use to hide a col. (index no.)at runtime
        PrintDocument1.OriginAtMargins = False ' takes margins into account 
        PageSetUpDialog1.PageSettings = DefaultPageSettings ' Displays page setting dialog-removed due to problems
        DefaultPageSettings.Landscape = True
        PageSetUpDialog1.ShowDialog()                        ' with margins when paper orientation changed

        Dim PrintPreviewDialog1 As New PrintPreviewDialog()

        PrintDocument1.DefaultPageSettings = PageSetUpDialog1.PageSettings ' Sets page settings to those from PageSetupDialog
        PrintPreviewDialog1.ClientSize = New System.Drawing.Size(1000, 700) ' Set Dialog box initial size
        PrintPreviewDialog1.Document = PrintDocument1 ' Previews print
        PrintPreviewDialog1.ShowDialog()


        'PrintDocument1.Print() ' Alternative for direct print
    End Sub

    Public Sub DocToPrint_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles DocToPrint.BeginPrint
        oStringFormat = New StringFormat
        oStringFormat.Alignment = StringAlignment.Near
        oStringFormat.LineAlignment = StringAlignment.Center
        oStringFormat.Trimming = StringTrimming.EllipsisCharacter
        oStringFormatComboBox = New StringFormat
        oStringFormatComboBox.LineAlignment = StringAlignment.Center
        oStringFormatComboBox.FormatFlags = StringFormatFlags.NoWrap
        oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
        oButton = New Button
        oCheckbox = New CheckBox
        oComboBox = New ComboBox
        nTotalWidth = 0
        For Each oColumn As DataGridViewColumn In DataGridView1.Columns
            If oColumn.Visible = True Then ' Prints only Visible columns
                nTotalWidth += oColumn.Width
            End If
        Next
        nPageNo = 1
        NewPage = True
        nRowPos = 0

    End Sub
    Public Sub DocToPrint_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
        'Not currently used
    End Sub

    Public Sub DocToPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles DocToPrint.PrintPage
        Header = "Overzicht"
        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)), Int32)
                    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
                    e.Graphics.DrawString(Header, 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 CellSize As Rectangle = New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)
                            Dim ImgSize As Size = CType(oCell.FormattedValue, Image).Size
                            e.Graphics.DrawImage(oCell.FormattedValue, New Rectangle(oColumnLefts(i) + CType(((CellSize.Width - ImgSize.Width) / 2), Int32), _
                                   nTop + CType(((CellSize.Height - ImgSize.Height) / 2),  _
                                    Int32), CType(oCell.FormattedValue, Image).Width, CType(oCell.FormattedValue,  _
                                    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


    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 - User Name
        e.Graphics.DrawString(sUserName, 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) '4 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
End Module
 
dit is allemaal gelukt maar als ik nu wil printen verdwijnt column3 :confused:
weet iemand hoe dit kan ?
 
nee dat niet
ik heb gewoon 4 colommen, maar als ik print dan verdwijnt de derde :confused:
 
nee het is nu gewoon nog een textbox
heeft het misschien hier mee te maken ?

Code:
DataGridView1.Columns(2).Visible = False
 
Laatst bewerkt:
Welzeker coloms begind bij 0 dus je derde colom staat op False zet op true
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan