Hi, below is the code i got to print a datagridview, the trouble is it also prints visible columns which i don't want to be printed. Does anyone have any ideas on how i could change the code so it won't print these columns?
[CODE=vbnet]
Private Sub PrintDocument1_ BeginPrint(ByVa l sender As Object, ByVal e As System.Drawing. Printing.PrintE ventArgs) Handles PrintDocument1. BeginPrint
datagridprint = DataGridView1
oStringFormat = New StringFormat
oStringFormat.A lignment = StringAlignment .Near
oStringFormat.L ineAlignment = StringAlignment .Center
oStringFormat.T rimming = StringTrimming. EllipsisCharact er
oStringFormatCo mboBox = New StringFormat
oStringFormatCo mboBox.LineAlig nment = StringAlignment .Center
oStringFormatCo mboBox.FormatFl ags = StringFormatFla gs.NoWrap
oStringFormatCo mboBox.Trimming = StringTrimming. EllipsisCharact er
oButton = New Button
oCheckbox = New CheckBox
oComboBox = New ComboBox
nTotalWidth = 0
For Each oColumn As DataGridViewCol umn In datagridprint.C olumns
nTotalWidth += oColumn.Width
Next
nPageNo = 1
NewPage = True
nRowPos = 0
End Sub
Private Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As System.Drawing. Printing.PrintP ageEventArgs) Handles PrintDocument1. PrintPage
datagridprint = DataGridView1
e.Graphics.Draw Image(My.Resour ces.dtdtitle, 275, 25)
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
For Each oColumn As DataGridViewCol umn In datagridprint.C olumns
nWidth = CType(Math.Floo r(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds .Width / nTotalWidth)), Int16)
nHeight = e.Graphics.Meas ureString(oColu mn.HeaderText, oColumn.Inherit edStyle.Font, nWidth).Height + 11
oColumnLefts.Ad d(nLeft)
oColumnWidths.A dd(nWidth)
oColumnTypes.Ad d(oColumn.GetTy pe)
nLeft += nWidth
Next
End If
Do While nRowPos < datagridprint.R ows.Count - 1
Dim oRow As DataGridViewRow = datagridprint.R ows(nRowPos)
If nTop + nHeight >= e.MarginBounds. Height + e.MarginBounds. Top Then
NewPage = True
nPageNo += 1
e.HasMorePages = True
Exit Sub
Else
If NewPage Then
' Draw Columns
nTop = 125
i = 0
For Each oColumn As DataGridViewCol umn In datagridprint.C olumns
e.Graphics.Fill Rectangle(New SolidBrush(Draw ing.Color.Light Gray), New Rectangle(oColu mnLefts(i), nTop, oColumnWidths(i ), nHeight))
e.Graphics.Draw Rectangle(Pens. Black, New Rectangle(oColu mnLefts(i), nTop, oColumnWidths(i ), nHeight))
e.Graphics.Draw String(oColumn. HeaderText, oColumn.Inherit edStyle.Font, New SolidBrush(oCol umn.InheritedSt yle.ForeColor), New RectangleF(oCol umnLefts(i), nTop, oColumnWidths(i ), nHeight), oStringFormat)
i += 1
Next
NewPage = False
End If
nTop += nHeight
i = 0
For Each oCell As DataGridViewCel l In oRow.Cells
If oColumnTypes(i) Is GetType(DataGri dViewTextBoxCol umn) OrElse oColumnTypes(i) Is GetType(DataGri dViewLinkColumn ) Then
e.Graphics.Draw String(oCell.Va lue.ToString, oCell.Inherited Style.Font, New SolidBrush(oCel l.InheritedStyl e.ForeColor), New RectangleF(oCol umnLefts(i), nTop, oColumnWidths(i ), nHeight), oStringFormat)
ElseIf oColumnTypes(i) Is GetType(DataGri dViewButtonColu mn) Then
oButton.Text = oCell.Value.ToS tring
oButton.Size = New Size(oColumnWid ths(i), nHeight)
Dim oBitmap As New Bitmap(oButton. Width, oButton.Height)
oButton.DrawToB itmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height) )
e.Graphics.Draw Image(oBitmap, New Point(oColumnLe fts(i), nTop))
ElseIf oColumnTypes(i) Is GetType(DataGri dViewCheckBoxCo lumn) Then
oCheckbox.Size = New Size(14, 14)
oCheckbox.Check ed = CType(oCell.Val ue, Boolean)
Dim oBitmap As New Bitmap(oColumnW idths(i), nHeight)
Dim oTempGraphics As Graphics = Graphics.FromIm age(oBitmap)
oTempGraphics.F illRectangle(Br ushes.White, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height) )
oCheckbox.DrawT oBitmap(oBitmap , New Rectangle(CType ((oBitmap.Width - oCheckbox.Width ) / 2, Int32), CType((oBitmap. Height - oCheckbox.Heigh t) / 2, Int32), oCheckbox.Width , oCheckbox.Heigh t))
e.Graphics.Draw Image(oBitmap, New Point(oColumnLe fts(i), nTop))
ElseIf oColumnTypes(i) Is GetType(DataGri dViewComboBoxCo lumn) Then
oComboBox.Size = New Size(oColumnWid ths(i), nHeight)
Dim oBitmap As New Bitmap(oComboBo x.Width, oComboBox.Heigh t)
oComboBox.DrawT oBitmap(oBitmap , New Rectangle(0, 0, oBitmap.Width, oBitmap.Height) )
e.Graphics.Draw Image(oBitmap, New Point(oColumnLe fts(i), nTop))
e.Graphics.Draw String(oCell.Va lue.ToString, oCell.Inherited Style.Font, New SolidBrush(oCel l.InheritedStyl e.ForeColor), New RectangleF(oCol umnLefts(i) + 1, nTop, oColumnWidths(i ) - 16, nHeight), oStringFormatCo mboBox)
ElseIf oColumnTypes(i) Is GetType(DataGri dViewImageColum n) Then
Dim oCellSize As Rectangle = New Rectangle(oColu mnLefts(i), nTop, oColumnWidths(i ), nHeight)
Dim oImageSize As Size = CType(oCell.Val ue, Image).Size
e.Graphics.Draw Image(oCell.Val ue, New Rectangle(oColu mnLefts(i) + CType(((oCellSi ze.Width - oImageSize.Widt h) / 2), Int32), nTop + CType(((oCellSi ze.Height - oImageSize.Heig ht) / 2), Int32), CType(oCell.Val ue, Image).Width, CType(oCell.Val ue, Image).Height))
End If
e.Graphics.Draw Rectangle(Pens. Black, New Rectangle(oColu mnLefts(i), nTop, oColumnWidths(i ), nHeight))
i += 1
Next
End If
nRowPos += 1
nRowsPerPage += 1
Loop
e.HasMorePages = False
End Sub
[/CODE]
Thanks
[CODE=vbnet]
Private Sub PrintDocument1_ BeginPrint(ByVa l sender As Object, ByVal e As System.Drawing. Printing.PrintE ventArgs) Handles PrintDocument1. BeginPrint
datagridprint = DataGridView1
oStringFormat = New StringFormat
oStringFormat.A lignment = StringAlignment .Near
oStringFormat.L ineAlignment = StringAlignment .Center
oStringFormat.T rimming = StringTrimming. EllipsisCharact er
oStringFormatCo mboBox = New StringFormat
oStringFormatCo mboBox.LineAlig nment = StringAlignment .Center
oStringFormatCo mboBox.FormatFl ags = StringFormatFla gs.NoWrap
oStringFormatCo mboBox.Trimming = StringTrimming. EllipsisCharact er
oButton = New Button
oCheckbox = New CheckBox
oComboBox = New ComboBox
nTotalWidth = 0
For Each oColumn As DataGridViewCol umn In datagridprint.C olumns
nTotalWidth += oColumn.Width
Next
nPageNo = 1
NewPage = True
nRowPos = 0
End Sub
Private Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As System.Drawing. Printing.PrintP ageEventArgs) Handles PrintDocument1. PrintPage
datagridprint = DataGridView1
e.Graphics.Draw Image(My.Resour ces.dtdtitle, 275, 25)
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
For Each oColumn As DataGridViewCol umn In datagridprint.C olumns
nWidth = CType(Math.Floo r(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds .Width / nTotalWidth)), Int16)
nHeight = e.Graphics.Meas ureString(oColu mn.HeaderText, oColumn.Inherit edStyle.Font, nWidth).Height + 11
oColumnLefts.Ad d(nLeft)
oColumnWidths.A dd(nWidth)
oColumnTypes.Ad d(oColumn.GetTy pe)
nLeft += nWidth
Next
End If
Do While nRowPos < datagridprint.R ows.Count - 1
Dim oRow As DataGridViewRow = datagridprint.R ows(nRowPos)
If nTop + nHeight >= e.MarginBounds. Height + e.MarginBounds. Top Then
NewPage = True
nPageNo += 1
e.HasMorePages = True
Exit Sub
Else
If NewPage Then
' Draw Columns
nTop = 125
i = 0
For Each oColumn As DataGridViewCol umn In datagridprint.C olumns
e.Graphics.Fill Rectangle(New SolidBrush(Draw ing.Color.Light Gray), New Rectangle(oColu mnLefts(i), nTop, oColumnWidths(i ), nHeight))
e.Graphics.Draw Rectangle(Pens. Black, New Rectangle(oColu mnLefts(i), nTop, oColumnWidths(i ), nHeight))
e.Graphics.Draw String(oColumn. HeaderText, oColumn.Inherit edStyle.Font, New SolidBrush(oCol umn.InheritedSt yle.ForeColor), New RectangleF(oCol umnLefts(i), nTop, oColumnWidths(i ), nHeight), oStringFormat)
i += 1
Next
NewPage = False
End If
nTop += nHeight
i = 0
For Each oCell As DataGridViewCel l In oRow.Cells
If oColumnTypes(i) Is GetType(DataGri dViewTextBoxCol umn) OrElse oColumnTypes(i) Is GetType(DataGri dViewLinkColumn ) Then
e.Graphics.Draw String(oCell.Va lue.ToString, oCell.Inherited Style.Font, New SolidBrush(oCel l.InheritedStyl e.ForeColor), New RectangleF(oCol umnLefts(i), nTop, oColumnWidths(i ), nHeight), oStringFormat)
ElseIf oColumnTypes(i) Is GetType(DataGri dViewButtonColu mn) Then
oButton.Text = oCell.Value.ToS tring
oButton.Size = New Size(oColumnWid ths(i), nHeight)
Dim oBitmap As New Bitmap(oButton. Width, oButton.Height)
oButton.DrawToB itmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height) )
e.Graphics.Draw Image(oBitmap, New Point(oColumnLe fts(i), nTop))
ElseIf oColumnTypes(i) Is GetType(DataGri dViewCheckBoxCo lumn) Then
oCheckbox.Size = New Size(14, 14)
oCheckbox.Check ed = CType(oCell.Val ue, Boolean)
Dim oBitmap As New Bitmap(oColumnW idths(i), nHeight)
Dim oTempGraphics As Graphics = Graphics.FromIm age(oBitmap)
oTempGraphics.F illRectangle(Br ushes.White, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height) )
oCheckbox.DrawT oBitmap(oBitmap , New Rectangle(CType ((oBitmap.Width - oCheckbox.Width ) / 2, Int32), CType((oBitmap. Height - oCheckbox.Heigh t) / 2, Int32), oCheckbox.Width , oCheckbox.Heigh t))
e.Graphics.Draw Image(oBitmap, New Point(oColumnLe fts(i), nTop))
ElseIf oColumnTypes(i) Is GetType(DataGri dViewComboBoxCo lumn) Then
oComboBox.Size = New Size(oColumnWid ths(i), nHeight)
Dim oBitmap As New Bitmap(oComboBo x.Width, oComboBox.Heigh t)
oComboBox.DrawT oBitmap(oBitmap , New Rectangle(0, 0, oBitmap.Width, oBitmap.Height) )
e.Graphics.Draw Image(oBitmap, New Point(oColumnLe fts(i), nTop))
e.Graphics.Draw String(oCell.Va lue.ToString, oCell.Inherited Style.Font, New SolidBrush(oCel l.InheritedStyl e.ForeColor), New RectangleF(oCol umnLefts(i) + 1, nTop, oColumnWidths(i ) - 16, nHeight), oStringFormatCo mboBox)
ElseIf oColumnTypes(i) Is GetType(DataGri dViewImageColum n) Then
Dim oCellSize As Rectangle = New Rectangle(oColu mnLefts(i), nTop, oColumnWidths(i ), nHeight)
Dim oImageSize As Size = CType(oCell.Val ue, Image).Size
e.Graphics.Draw Image(oCell.Val ue, New Rectangle(oColu mnLefts(i) + CType(((oCellSi ze.Width - oImageSize.Widt h) / 2), Int32), nTop + CType(((oCellSi ze.Height - oImageSize.Heig ht) / 2), Int32), CType(oCell.Val ue, Image).Width, CType(oCell.Val ue, Image).Height))
End If
e.Graphics.Draw Rectangle(Pens. Black, New Rectangle(oColu mnLefts(i), nTop, oColumnWidths(i ), nHeight))
i += 1
Next
End If
nRowPos += 1
nRowsPerPage += 1
Loop
e.HasMorePages = False
End Sub
[/CODE]
Thanks
Comment