Private Sub Calculate(ByVal g As Graphics) If PageNumber = 0 Then ' Just calculate once
Dim tmpSize As SizeF = New SizeF()
Dim tmpFont As Font
Dim tmpWidth As Single
Dim i As Integer
Dim j As Integer
TheDataGridView.RightToLeft = RightToLeft.Yes
TheDataGridViewWidth = 0
For i = 0 To TheDataGridView.Columns.Count - 1
tmpFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font
If tmpFont Is Nothing Then ' If there is no special HeaderFont style, then use the default DataGridView font style
tmpFont = TheDataGridView.DefaultCellStyle.Font
End If
tmpSize = g.MeasureString(TheDataGridView.Columns(i).HeaderText, tmpFont)
tmpWidth = tmpSize.Width
RowHeaderHeight = tmpSize.Height
For j = 0 To TheDataGridView.Rows.Count - 1
tmpFont = TheDataGridView.Rows(j).DefaultCellStyle.Font
If tmpFont Is Nothing Then ' If the there is no special font style of the CurrentRow, then use the default one associated with the DataGridView control
tmpFont = TheDataGridView.DefaultCellStyle.Font
End If
tmpSize = g.MeasureString("Anything", tmpFont)
RowsHeight.Add(tmpSize.Height)
tmpSize = g.MeasureString(TheDataGridView.Rows(j).Cells(i).EditedFormattedValue.ToString(), tmpFont)
If (tmpSize.Width > tmpWidth) Then
tmpWidth = tmpSize.Width
End If
Next
If TheDataGridView.Columns(i).Visible Then
TheDataGridViewWidth += tmpWidth
End If
ColumnsWidth.Add(tmpWidth)
Next
' Define the start/stop column points based on the page width and the DataGridView Width
' We will use this to determine the columns which are drawn on each page and how wrapping will be handled
' By default, the wrapping will occurr such that the maximum number of columns for a page will be determine
Dim k As Integer
Dim mStartPoint As Integer = 0
For k = TheDataGridView.Columns.Count - 1 To 0 Step -1
If TheDataGridView.Columns(k).Visible Then
mStartPoint = k
Exit For
End If
Next
Dim mEndPoint As Integer = TheDataGridView.Columns.Count
For k = 0 To TheDataGridView.Columns.Count - 1
If TheDataGridView.Columns(k).Visible Then
mEndPoint = k + 1
Exit For
End If
Next
Dim mTempWidth As Single = TheDataGridViewWidth
Dim mTempPrintArea As Single = CType(PageWidth, Single) - CType(LeftMargin, Single) - CType(RightMargin, Single)
' We only care about handling where the total datagridview width is bigger then the print area
If TheDataGridViewWidth > mTempPrintArea Then
mTempWidth = 0.0
For k = 0 To TheDataGridView.Columns.Count - 1
If TheDataGridView.Columns(k).Visible Then
mTempWidth += ColumnsWidth(k)
' If the width is bigger than the page area, then define a new column print range
If (mTempWidth > mTempPrintArea) Then
mTempWidth -= ColumnsWidth(k)
mColumnPoints.Add(New Integer() {mStartPoint, mEndPoint})
mColumnPointsWidth.Add(mTempWidth)
mStartPoint = k
mTempWidth = ColumnsWidth(k)
End If
End If
' Our end point is actually one index above the current index
mEndPoint = k + 1
Next
End If
' Add the last set of columns
mColumnPoints.Add(New Integer() {mStartPoint, mEndPoint})
mColumnPointsWidth.Add(mTempWidth)
mColumnPoint = 0
End If
End Sub