0
下面的代碼實際上是從其中一個stackoverflow答案中複製的,它不顯示打印預覽中的第一行。它跳過第一排,從第二排開始。這段代碼有什麼問題?它沒有顯示PrintPreview中的第一行
這裏的代碼,
Dim mRow As Integer = 0
Dim newpage As Boolean = True
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
newpage = True
With dgvData
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
'Dim fmt2 As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
'Dim font As Font
'font = New Font("Microsoft Sans Serif", "13", FontStyle.Underline, GraphicsUnit.Inch, gdiCharSet:=1)
'fmt.Trimming = StringTrimming.EllipsisCharacter
Dim rc1 As RectangleF = New RectangleF(460, 20, 350, 20)
Dim rc2 As RectangleF = New RectangleF(500, 40, 350, 20)
e.Graphics.DrawString("Some Heading", .Font, Brushes.Black, rc1, fmt)
e.Graphics.DrawString("Another heading ", .Font, Brushes.Black, rc2, fmt)
Dim y As Single = e.MarginBounds.Top + 60
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If (newpage) Then
e.Graphics.DrawString(dgvData.Columns(cell.ColumnIndex).HeaderText.ToString, .Font, Brushes.Black, rc, fmt)
'MessageBox.Show(dgvData.Columns(cell.ColumnIndex).HeaderText.ToString)
Else
e.Graphics.DrawString(dgvData.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
'MessageBox.Show(dgvData.Rows(cell.RowIndex).ToString)
End If
x += rc.Width
h = Math.Max(h, rc.Height)
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages = True
mRow -= 1
newpage = True
Exit Sub
End If
Loop
mRow = 0
End With
End Sub
所以我應該在開始時設置newage = false,還是應該做其他事情? – PhpCoder
@YiiFighter - 我編輯了我的答案。 'newpage'的值並不是問題,如果'newpage'爲True,那麼你正在放棄標題的當前行的標準輸出。 –
謝謝你的朋友..你的修改解決了這個問題.... – PhpCoder