Sean Rhudy
asked on
PRINTING A DATAGRID
Hello, I have a datagrid called eodgrid on a form called endofday. I have a program called Visual Documents that usually makes printing much easier, but I am stuck on this one. The following line is giving me an error " 'Table' is not a member of 'System.Windows.Forms.Data grid' "
Dim aFilter As String = Me.eodgrid.Table(myTableIn dex).Defau ltView.Row Filter.Tri m
Here is all of the code:
Imports System.Data
Public Class Endofday
'
'Declarations to print the Grid
'
Private myLastColPrinted As Integer = -1
Private myPageWidthExceeded As Boolean = False
Private myPageNo As Integer = 0
Private myLinesPrintedSoFar As Integer = 0
Private myRowsProcessedSoFar As Integer = -1
Private myDataSetRows As Integer = 0
Private myTableIndex As Integer = 0
Private myFilteredRows As DataRow()
Private myDatasetFiltered As Boolean = False
Private myDrawBrush As New SolidBrush(Color.Black) 'Create the default brush
Private myDrawFont As New Font("Courier New", 8) 'Modify this line to change the font
Dim con As New OleDb.OleDbConnection
Dim sql As String
Dim ds As New DataSet
Private Cmd As New OleDb.OleDbCommand
Dim da As OleDb.OleDbDataAdapter
Private Sub Endofday_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ReceiptDataSet5.totalquer y' table. You can move, or remove it, as needed.
Me.TotalqueryTableAdapter. Fill(Me.Re ceiptDataS et5.totalq uery)
'TODO: This line of code loads data into the 'ReceiptDataSet4.pdatequer y' table. You can move, or remove it, as needed.
Me.PdatequeryTableAdapter. Fill(Me.Re ceiptDataS et4.pdateq uery)
Me.TotalqueryTableAdapter. Fill(Me.Re ceiptDataS et5.totalq uery)
con.ConnectionString = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source=c:/backup/receiptpr ogram/rece iptmanagem ent/receip t.mdb"
con.Open()
Dsendofday.Clear()
'Cmd.CommandText = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo WHERE pdate = null"
'Cmd.CommandType = CommandType.Text
'da = New OleDb.OleDbDataAdapter(Cmd )
'da.Fill(Dsendofday.Receip tinfo)
con.Close()
End Sub
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Me.Visible = False
frontpage.Visible = True
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try
AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPag e
PrintDocument1.Print()
Catch ex As Exception
MessageBox.Show("An error occurred while printing", ex.ToString())
End Try
End Sub
Private Sub PrintDocument1_PrintPage(B yVal sender As System.Object, ByVal e As System.Drawing.Printing.Pr intPageEve ntArgs) Handles PrintDocument1.PrintPage
Dim myNumberOfColumns As Integer
Dim aTblIndex As Integer = 0
Dim myDataSetToPrint As DataSet = Me.ReceiptDataSet4 'Set here the DataSet to print
Dim GridLeft As Single = 0.2395833 'The left position of the Grid
Dim GridRight As Single = 8 'The Right position of the Grid
Dim Gridtop As Single = 1.3125 'The top position of the Grid
Dim GridBottom As Single = 7.885417 'The bottom position of the Grid
Dim aScaleFactor As Single = 14.45
Dim aLeftMargin As Integer = Int((GridLeft * 1440) / aScaleFactor)
Dim aRightMargin As Integer = Int((GridRight * 1440) / aScaleFactor)
Dim aTopMargin As Integer = Int((Gridtop * 1440) / aScaleFactor)
Dim aBottomMargin As Integer = Int((GridBottom * 1440) / aScaleFactor)
Dim myLineThreshold As Integer = 500
Dim aFilter As String = Me.eodgrid.Table(myTableIn dex).Defau ltView.Row Filter.Tri m
Dim aValue As String
Dim aTextHeight As Single = myDrawFont.GetHeight(e.Gra phics)
Dim aXPos As Single = aLeftMargin
Dim aYPos As Single = aTopMargin
Dim aColIndex As Integer = 0
Dim aRowIndex As Integer
Dim aLinesFilled As Integer
Dim aLinesPerPage As Integer = 0
Dim aCharactersFitted As Integer
Dim aLinesPrintedThisPage As Integer = 0
Dim aColsToPrint As Integer
Dim aLinesPrintedSoFar As Integer = myLinesPrintedSoFar
Dim aFirstColToPrint As Integer
Dim aRow As DataRow
Dim aCol As DataColumn
Dim aStringSize As New SizeF
Dim aLayoutSize As New SizeF
Dim aNewStringFormat As New StringFormat
Dim aPrintIt As Boolean
Dim GridLinesColor As Pen = New Pen(Color.FromArgb(255, 128, 128, 128)) 'Draw gridlines in Gray
Dim DrawGridLines As Boolean = True 'Set True or False to show or hide the gridlines
Dim myfont As Font
Dim myBrush As SolidBrush
Dim MyPen As Pen
Dim X1 As Single, X2 As Single, Y1 As Single, Y2 As Single
myNumberOfColumns = myDataSetToPrint.Tables(aT blIndex).C olumns.Cou nt - 1
myPageNo = 0
myLinesPrintedSoFar = 0
If aFilter = "" Then
myDatasetFiltered = False
myDataSetRows = myDataSetToPrint.Tables(my TableIndex ).Rows.Cou nt - 1
Else
myDatasetFiltered = True
myDataSetRows = myDataSetToPrint.Tables(my TableIndex ).DefaultV iew.Count - 1
myFilteredRows = myDataSetToPrint.Tables(my TableIndex ).Select(a Filter)
End If
e.Graphics.PageUnit = GraphicsUnit.Display
With myDataSetToPrint.Tables(my TableIndex ).Columns
If myNumberOfColumns = 0 Then
aColsToPrint = .Count
Else
If myNumberOfColumns > .Count Then
aColsToPrint = .Count
Else
aColsToPrint = myNumberOfColumns
End If
End If
End With
aLayoutSize.Height = aTextHeight
aLinesPerPage = Int((aBottomMargin - aTopMargin) / aTextHeight)
aNewStringFormat.FormatFla gs = StringFormatFlags.NoWrap
myPageNo += 1
'Print the rest of the objects
e.Graphics.PageUnit = GraphicsUnit.Inch
'
'Print Captions
'
myBrush = New SolidBrush(Color.FromArgb( 255, 18, 0, 0))
myfont = New Font("MS Sans Serif", 14, FontStyle.Regular)
e.Graphics.DrawString("END OF DAY", myfont, myBrush, 3.84375, 0.2604167)
'Print the dataset
e.Graphics.PageUnit = GraphicsUnit.Display
aLayoutSize.Width = aRightMargin - aLeftMargin
myPageWidthExceeded = False
aFirstColToPrint = myLastColPrinted + 1
For aColIndex = aFirstColToPrint To aColsToPrint
aCol = myDataSetToPrint.Tables(my TableIndex ).Columns( aColIndex)
aPrintIt = True
aStringSize = e.Graphics.MeasureString(a Col.Captio n, myDrawFont)
aLayoutSize.Width = (aRightMargin - aLeftMargin) / (aColsToPrint + 1)
aStringSize = e.Graphics.MeasureString(a Col.Captio n, myDrawFont, _
aLayoutSize, aNewStringFormat, aCharactersFitted, aLinesFilled)
If aPrintIt Then
If (aXPos + aLayoutSize.Width) <= aRightMargin Then
e.Graphics.DrawString(Mid( aCol.Capti on, 1, aCharactersFitted), myDrawFont, myDrawBrush, aXPos, aYPos)
aXPos += aLayoutSize.Width
myLastColPrinted = aColIndex
Else
myPageWidthExceeded = True
Exit For
End If
Else
myLastColPrinted = aColIndex
End If
Next
aLinesPrintedThisPage += 1
For aRowIndex = myLinesPrintedSoFar To myDataSetRows
If myDatasetFiltered Then
aRow = myFilteredRows(aRowIndex)
Else
aRow = myDataSetToPrint.Tables(my TableIndex ).Rows.Ite m(aRowInde x)
End If
myRowsProcessedSoFar += 1
If aRow.RowState <> DataRowState.Deleted Then
If DrawGridLines Then e.Graphics.DrawLine(GridLi nesColor, aLeftMargin, aYPos, aRightMargin, aYPos)
aXPos = aLeftMargin
aYPos += aTextHeight
For aColIndex = aFirstColToPrint To myLastColPrinted
aValue = aRow(aColIndex).ToString
aCol = myDataSetToPrint.Tables(my TableIndex ).Columns( aColIndex)
aStringSize = e.Graphics.MeasureString(a Value, myDrawFont, aLayoutSize, aNewStringFormat, aCharactersFitted, aLinesFilled)
If aPrintIt Then
If aValue.Length > aCharactersFitted Then
aValue = Mid(aValue, 1, aCharactersFitted)
End If
If DrawGridLines Then e.Graphics.DrawLine(GridLi nesColor, aXPos, aYPos - aTextHeight, aXPos, aYPos + aTextHeight)
e.Graphics.DrawString(aVal ue, myDrawFont, myDrawBrush, aXPos, aYPos)
aXPos += aLayoutSize.Width
aLayoutSize.Height = aYPos + aTextHeight
End If
Next
If DrawGridLines Then
e.Graphics.DrawLine(GridLi nesColor, aXPos, aYPos - aTextHeight, aXPos, aYPos + aTextHeight)
e.Graphics.DrawLine(GridLi nesColor, aLeftMargin, aYPos, aRightMargin, aYPos)
e.Graphics.DrawLine(GridLi nesColor, aLeftMargin, aYPos + aTextHeight, aRightMargin, aYPos + aTextHeight)
End If
aLinesPrintedThisPage += 1
myLinesPrintedSoFar += 1
If aLinesPrintedThisPage > aLinesPerPage Then Exit For
If myLinesPrintedSoFar > myDataSetRows Then Exit For
End If
If myRowsProcessedSoFar > myDataSetRows Then Exit For
Next
If myPageWidthExceeded Then
myLinesPrintedSoFar = aLinesPrintedSoFar
End If
If myLastColPrinted >= aColsToPrint Then
myLastColPrinted = -1
End If
If myLinesPrintedSoFar <= myDataSetRows And myRowsProcessedSoFar < myDataSetRows Then
e.HasMorePages = True
Else
e.HasMorePages = False
myPageNo = 0
myLinesPrintedSoFar = 0
myRowsProcessedSoFar = -1
myLastColPrinted = -1
End If
aCol = Nothing
aRow = Nothing
Exit Sub
ErrorHandler:
Throw New Exception("Error formatting report output.", Err.GetException)
End Sub
End Class
Dim aFilter As String = Me.eodgrid.Table(myTableIn
Here is all of the code:
Imports System.Data
Public Class Endofday
'
'Declarations to print the Grid
'
Private myLastColPrinted As Integer = -1
Private myPageWidthExceeded As Boolean = False
Private myPageNo As Integer = 0
Private myLinesPrintedSoFar As Integer = 0
Private myRowsProcessedSoFar As Integer = -1
Private myDataSetRows As Integer = 0
Private myTableIndex As Integer = 0
Private myFilteredRows As DataRow()
Private myDatasetFiltered As Boolean = False
Private myDrawBrush As New SolidBrush(Color.Black) 'Create the default brush
Private myDrawFont As New Font("Courier New", 8) 'Modify this line to change the font
Dim con As New OleDb.OleDbConnection
Dim sql As String
Dim ds As New DataSet
Private Cmd As New OleDb.OleDbCommand
Dim da As OleDb.OleDbDataAdapter
Private Sub Endofday_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ReceiptDataSet5.totalquer
Me.TotalqueryTableAdapter.
'TODO: This line of code loads data into the 'ReceiptDataSet4.pdatequer
Me.PdatequeryTableAdapter.
Me.TotalqueryTableAdapter.
con.ConnectionString = "Provider=Microsoft.Jet.OL
con.Open()
Dsendofday.Clear()
'Cmd.CommandText = "SELECT ReceiptNo, PolicyNo, pmt1, pmt2, pmt3, type1, type2, type3, total, rcvdby, firstname, lastname, typeaccount, company, checkmaker, checkno, Datercvd, notebox FROM Receiptinfo WHERE pdate = null"
'Cmd.CommandType = CommandType.Text
'da = New OleDb.OleDbDataAdapter(Cmd
'da.Fill(Dsendofday.Receip
con.Close()
End Sub
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Me.Visible = False
frontpage.Visible = True
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try
AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPag
PrintDocument1.Print()
Catch ex As Exception
MessageBox.Show("An error occurred while printing", ex.ToString())
End Try
End Sub
Private Sub PrintDocument1_PrintPage(B
Dim myNumberOfColumns As Integer
Dim aTblIndex As Integer = 0
Dim myDataSetToPrint As DataSet = Me.ReceiptDataSet4 'Set here the DataSet to print
Dim GridLeft As Single = 0.2395833 'The left position of the Grid
Dim GridRight As Single = 8 'The Right position of the Grid
Dim Gridtop As Single = 1.3125 'The top position of the Grid
Dim GridBottom As Single = 7.885417 'The bottom position of the Grid
Dim aScaleFactor As Single = 14.45
Dim aLeftMargin As Integer = Int((GridLeft * 1440) / aScaleFactor)
Dim aRightMargin As Integer = Int((GridRight * 1440) / aScaleFactor)
Dim aTopMargin As Integer = Int((Gridtop * 1440) / aScaleFactor)
Dim aBottomMargin As Integer = Int((GridBottom * 1440) / aScaleFactor)
Dim myLineThreshold As Integer = 500
Dim aFilter As String = Me.eodgrid.Table(myTableIn
Dim aValue As String
Dim aTextHeight As Single = myDrawFont.GetHeight(e.Gra
Dim aXPos As Single = aLeftMargin
Dim aYPos As Single = aTopMargin
Dim aColIndex As Integer = 0
Dim aRowIndex As Integer
Dim aLinesFilled As Integer
Dim aLinesPerPage As Integer = 0
Dim aCharactersFitted As Integer
Dim aLinesPrintedThisPage As Integer = 0
Dim aColsToPrint As Integer
Dim aLinesPrintedSoFar As Integer = myLinesPrintedSoFar
Dim aFirstColToPrint As Integer
Dim aRow As DataRow
Dim aCol As DataColumn
Dim aStringSize As New SizeF
Dim aLayoutSize As New SizeF
Dim aNewStringFormat As New StringFormat
Dim aPrintIt As Boolean
Dim GridLinesColor As Pen = New Pen(Color.FromArgb(255, 128, 128, 128)) 'Draw gridlines in Gray
Dim DrawGridLines As Boolean = True 'Set True or False to show or hide the gridlines
Dim myfont As Font
Dim myBrush As SolidBrush
Dim MyPen As Pen
Dim X1 As Single, X2 As Single, Y1 As Single, Y2 As Single
myNumberOfColumns = myDataSetToPrint.Tables(aT
myPageNo = 0
myLinesPrintedSoFar = 0
If aFilter = "" Then
myDatasetFiltered = False
myDataSetRows = myDataSetToPrint.Tables(my
Else
myDatasetFiltered = True
myDataSetRows = myDataSetToPrint.Tables(my
myFilteredRows = myDataSetToPrint.Tables(my
End If
e.Graphics.PageUnit = GraphicsUnit.Display
With myDataSetToPrint.Tables(my
If myNumberOfColumns = 0 Then
aColsToPrint = .Count
Else
If myNumberOfColumns > .Count Then
aColsToPrint = .Count
Else
aColsToPrint = myNumberOfColumns
End If
End If
End With
aLayoutSize.Height = aTextHeight
aLinesPerPage = Int((aBottomMargin - aTopMargin) / aTextHeight)
aNewStringFormat.FormatFla
myPageNo += 1
'Print the rest of the objects
e.Graphics.PageUnit = GraphicsUnit.Inch
'
'Print Captions
'
myBrush = New SolidBrush(Color.FromArgb(
myfont = New Font("MS Sans Serif", 14, FontStyle.Regular)
e.Graphics.DrawString("END
'Print the dataset
e.Graphics.PageUnit = GraphicsUnit.Display
aLayoutSize.Width = aRightMargin - aLeftMargin
myPageWidthExceeded = False
aFirstColToPrint = myLastColPrinted + 1
For aColIndex = aFirstColToPrint To aColsToPrint
aCol = myDataSetToPrint.Tables(my
aPrintIt = True
aStringSize = e.Graphics.MeasureString(a
aLayoutSize.Width = (aRightMargin - aLeftMargin) / (aColsToPrint + 1)
aStringSize = e.Graphics.MeasureString(a
aLayoutSize, aNewStringFormat, aCharactersFitted, aLinesFilled)
If aPrintIt Then
If (aXPos + aLayoutSize.Width) <= aRightMargin Then
e.Graphics.DrawString(Mid(
aXPos += aLayoutSize.Width
myLastColPrinted = aColIndex
Else
myPageWidthExceeded = True
Exit For
End If
Else
myLastColPrinted = aColIndex
End If
Next
aLinesPrintedThisPage += 1
For aRowIndex = myLinesPrintedSoFar To myDataSetRows
If myDatasetFiltered Then
aRow = myFilteredRows(aRowIndex)
Else
aRow = myDataSetToPrint.Tables(my
End If
myRowsProcessedSoFar += 1
If aRow.RowState <> DataRowState.Deleted Then
If DrawGridLines Then e.Graphics.DrawLine(GridLi
aXPos = aLeftMargin
aYPos += aTextHeight
For aColIndex = aFirstColToPrint To myLastColPrinted
aValue = aRow(aColIndex).ToString
aCol = myDataSetToPrint.Tables(my
aStringSize = e.Graphics.MeasureString(a
If aPrintIt Then
If aValue.Length > aCharactersFitted Then
aValue = Mid(aValue, 1, aCharactersFitted)
End If
If DrawGridLines Then e.Graphics.DrawLine(GridLi
e.Graphics.DrawString(aVal
aXPos += aLayoutSize.Width
aLayoutSize.Height = aYPos + aTextHeight
End If
Next
If DrawGridLines Then
e.Graphics.DrawLine(GridLi
e.Graphics.DrawLine(GridLi
e.Graphics.DrawLine(GridLi
End If
aLinesPrintedThisPage += 1
myLinesPrintedSoFar += 1
If aLinesPrintedThisPage > aLinesPerPage Then Exit For
If myLinesPrintedSoFar > myDataSetRows Then Exit For
End If
If myRowsProcessedSoFar > myDataSetRows Then Exit For
Next
If myPageWidthExceeded Then
myLinesPrintedSoFar = aLinesPrintedSoFar
End If
If myLastColPrinted >= aColsToPrint Then
myLastColPrinted = -1
End If
If myLinesPrintedSoFar <= myDataSetRows And myRowsProcessedSoFar < myDataSetRows Then
e.HasMorePages = True
Else
e.HasMorePages = False
myPageNo = 0
myLinesPrintedSoFar = 0
myRowsProcessedSoFar = -1
myLastColPrinted = -1
End If
aCol = Nothing
aRow = Nothing
Exit Sub
ErrorHandler:
Throw New Exception("Error formatting report output.", Err.GetException)
End Sub
End Class
If I'm not mistaken, you have to get to the table through the datasource?
ASKER
please elaborate.
ASKER
Ok, I've gone back to the basics, How can I make this code print in landscape?
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(B yVal sender As System.Object, ByVal e As System.Drawing.Printing.Pr intPageEve ntArgs) Handles PrintDocument1.PrintPage
Dim myPaintArgs As New PaintEventArgs(e.Graphics, New Rectangle(New _
Point(0, 0), Me.Size))
Me.InvokePaint(eodgrid, myPaintArgs)
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(B
Dim myPaintArgs As New PaintEventArgs(e.Graphics,
Point(0, 0), Me.Size))
Me.InvokePaint(eodgrid, myPaintArgs)
End Sub
ASKER
is this just printing a graphic of it?
ASKER
Please close question, I used the following code:
Private Sub PrintDocument1_BeginPrint( ByVal sender As Object, ByVal e As System.Drawing.Printing.Pr intEventAr gs) Handles PrintDocument1.BeginPrint
oStringFormat = New StringFormat
oStringFormat.Alignment = StringAlignment.Near
oStringFormat.LineAlignmen t = StringAlignment.Center
oStringFormat.Trimming = StringTrimming.EllipsisCha racter
oStringFormatComboBox = New StringFormat
oStringFormatComboBox.Line Alignment = StringAlignment.Center
oStringFormatComboBox.Form atFlags = StringFormatFlags.NoWrap
oStringFormatComboBox.Trim ming = StringTrimming.EllipsisCha racter
oButton = New Button
oCheckbox = New CheckBox
oComboBox = New ComboBox
nTotalWidth = 0
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
nTotalWidth += oColumn.Width
Next
nPageNo = 1
NewPage = True
nRowPos = 0
End Sub
Private Sub PrintDocument1_PrintPage(B yVal sender As Object, ByVal e As System.Drawing.Printing.Pr intPageEve ntArgs) Handles PrintDocument1.PrintPage
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
e.PageSettings.Landscape = True
If nPageNo = 1 Then
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
nWidth = CType(Math.Floor(oColumn.W idth / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)
nHeight = e.Graphics.MeasureString(o Column.Hea derText, oColumn.InheritedStyle.Fon t, nWidth).Height + 11
oColumnLefts.Add(nLeft)
oColumnWidths.Add(nWidth)
oColumnTypes.add(oColumn.G etType)
nLeft += nWidth
Next
End If
Do While nRowPos < DataGridView1.Rows.Count - 1
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(Head er, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(H eader, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Heig ht - 13)
' Draw Columns
nTop = e.MarginBounds.Top
i = 0
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
e.Graphics.FillRectangle(N ew SolidBrush(Drawing.Color.L ightGray), New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
e.Graphics.DrawRectangle(P ens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
e.Graphics.DrawString(oCol umn.Header Text, oColumn.InheritedStyle.Fon t, New SolidBrush(oColumn.Inherit edStyle.Fo reColor), New RectangleF(oColumnLefts(i) , nTop, oColumnWidths(i), nHeight), oStringFormat)
i += 1
Next
NewPage = False
End If
nTop += nHeight
i = 0
For Each oCell As DataGridViewCell In oRow.Cells
If oColumnTypes(i) Is GetType(DataGridViewTextBo xColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkCo lumn) Then
e.Graphics.DrawString(oCel l.Value.To String, oCell.InheritedStyle.Font, New SolidBrush(oCell.Inherited Style.Fore Color), New RectangleF(oColumnLefts(i) , nTop, oColumnWidths(i), nHeight), oStringFormat)
ElseIf oColumnTypes(i) Is GetType(DataGridViewButton Column) Then
oButton.Text = oCell.Value.ToString
oButton.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
oButton.DrawToBitmap(oBitm ap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
e.Graphics.DrawImage(oBitm ap, New Point(oColumnLefts(i), nTop))
ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckB oxColumn) 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.FillRectangl e(Brushes. White, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
oCheckbox.DrawToBitmap(oBi tmap, New Rectangle(CType((oBitmap.W idth - oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) / 2, Int32), oCheckbox.Width, oCheckbox.Height))
e.Graphics.DrawImage(oBitm ap, New Point(oColumnLefts(i), nTop))
ElseIf oColumnTypes(i) Is GetType(DataGridViewComboB oxColumn) Then
oComboBox.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
oComboBox.DrawToBitmap(oBi tmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
e.Graphics.DrawImage(oBitm ap, New Point(oColumnLefts(i), nTop))
e.Graphics.DrawString(oCel l.Value.To String, oCell.InheritedStyle.Font, New SolidBrush(oCell.Inherited Style.Fore Color), New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight), oStringFormatComboBox)
ElseIf oColumnTypes(i) Is GetType(DataGridViewImageC olumn) 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(P ens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
i += 1
Next
End If
nRowPos += 1
nRowsPerPage += 1
Loop
DrawFooter(e, nRowsPerPage)
e.HasMorePages = False
End Sub
Private Sub DrawFooter(ByVal e As System.Drawing.Printing.Pr intPageEve ntArgs, ByVal RowsPerPage As Int32)
Dim sPageNo As String
If blnGotNumPages = False Then
blnGotNumPages = True
strNumOfPages = Math.Ceiling(DataGridView1 .Rows.Coun t / RowsPerPage).ToString
sPageNo = nPageNo.ToString + " of " + Math.Ceiling(DataGridView1 .Rows.Coun t / RowsPerPage).ToString
Else
sPageNo = nPageNo.ToString + " of " + strNumOfPages
End If
' Right Align - User Name
e.Graphics.DrawString(sUse rName, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(s PageNo, DataGridView1.Font, e.MarginBounds.Width).Widt h), e.MarginBounds.Top + e.MarginBounds.Height + 7)
' Left Align - Date/Time
e.Graphics.DrawString(Now. ToLongDate String + " " + Now.ToShortTimeString, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height + 7)
' Center - Page No. Info
e.Graphics.DrawString(sPag eNo, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(s PageNo, DataGridView1.Font, e.MarginBounds.Width).Widt h) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31)
End Sub
Private Sub PrintDocument1_BeginPrint(
oStringFormat = New StringFormat
oStringFormat.Alignment = StringAlignment.Near
oStringFormat.LineAlignmen
oStringFormat.Trimming = StringTrimming.EllipsisCha
oStringFormatComboBox = New StringFormat
oStringFormatComboBox.Line
oStringFormatComboBox.Form
oStringFormatComboBox.Trim
oButton = New Button
oCheckbox = New CheckBox
oComboBox = New ComboBox
nTotalWidth = 0
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
nTotalWidth += oColumn.Width
Next
nPageNo = 1
NewPage = True
nRowPos = 0
End Sub
Private Sub PrintDocument1_PrintPage(B
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
e.PageSettings.Landscape = True
If nPageNo = 1 Then
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
nWidth = CType(Math.Floor(oColumn.W
nHeight = e.Graphics.MeasureString(o
oColumnLefts.Add(nLeft)
oColumnWidths.Add(nWidth)
oColumnTypes.add(oColumn.G
nLeft += nWidth
Next
End If
Do While nRowPos < DataGridView1.Rows.Count - 1
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(Head
' Draw Columns
nTop = e.MarginBounds.Top
i = 0
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
e.Graphics.FillRectangle(N
e.Graphics.DrawRectangle(P
e.Graphics.DrawString(oCol
i += 1
Next
NewPage = False
End If
nTop += nHeight
i = 0
For Each oCell As DataGridViewCell In oRow.Cells
If oColumnTypes(i) Is GetType(DataGridViewTextBo
e.Graphics.DrawString(oCel
ElseIf oColumnTypes(i) Is GetType(DataGridViewButton
oButton.Text = oCell.Value.ToString
oButton.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
oButton.DrawToBitmap(oBitm
e.Graphics.DrawImage(oBitm
ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckB
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.FillRectangl
oCheckbox.DrawToBitmap(oBi
e.Graphics.DrawImage(oBitm
ElseIf oColumnTypes(i) Is GetType(DataGridViewComboB
oComboBox.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
oComboBox.DrawToBitmap(oBi
e.Graphics.DrawImage(oBitm
e.Graphics.DrawString(oCel
ElseIf oColumnTypes(i) Is GetType(DataGridViewImageC
Dim oCellSize As Rectangle = New Rectangle(oColumnLefts(i),
Dim oImageSize As Size = CType(oCell.Value, Image).Size
e.Graphics.DrawImage(oCell
End If
e.Graphics.DrawRectangle(P
i += 1
Next
End If
nRowPos += 1
nRowsPerPage += 1
Loop
DrawFooter(e, nRowsPerPage)
e.HasMorePages = False
End Sub
Private Sub DrawFooter(ByVal e As System.Drawing.Printing.Pr
Dim sPageNo As String
If blnGotNumPages = False Then
blnGotNumPages = True
strNumOfPages = Math.Ceiling(DataGridView1
sPageNo = nPageNo.ToString + " of " + Math.Ceiling(DataGridView1
Else
sPageNo = nPageNo.ToString + " of " + strNumOfPages
End If
' Right Align - User Name
e.Graphics.DrawString(sUse
' Left Align - Date/Time
e.Graphics.DrawString(Now.
' Center - Page No. Info
e.Graphics.DrawString(sPag
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.