2015-05-07 21 views
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 

回答

1

[如果我正確理解這一點,]它跳過第一線,因爲newpage設置爲True,所以當你得到這個代碼:

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 

您正在打印標題,而不是當前行中的單元格(Dim row As DataGridViewRow = .Rows(mRow))。

編輯響應「所以我應該設置NEWAGE在啓動=假,還是有別的我應該怎麼辦?

如果newpage評估爲真,則需要輸出兩個標題和當前行。在原始代碼中,您正在使用If開關執行其中一個操作(從而跳過當前行的輸出)。我做了一些快速重組,下面是我全新的建議代碼,所以你可以比較。

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 rc As RectangleF 
     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 
      If (newpage) Then 
       For Each cell As DataGridViewCell In row.Cells 
        rc = New RectangleF(x, y, cell.Size.Width, cell.Size.Height) 
        e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height) 
        e.Graphics.DrawString(dgvData.Columns(cell.ColumnIndex).HeaderText.ToString, .Font, Brushes.Black, rc, fmt) 
        'MessageBox.Show(dgvData.Columns(cell.ColumnIndex).HeaderText.ToString) 
        x += rc.Width 
        h = Math.Max(h, rc.Height) 
       Next 
       newpage = False 
       y += h 
       x = e.MarginBounds.Left 
       h = 0 
      End If 
      For Each cell As DataGridViewCell In row.Cells 
       rc = New RectangleF(x, y, cell.Size.Width, cell.Size.Height) 
       e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height) 
       e.Graphics.DrawString(dgvData.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt) 
       'MessageBox.Show(dgvData.Rows(cell.RowIndex).ToString) 
       x += rc.Width 
       h = Math.Max(h, rc.Height) 
      Next 
      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 

一些變化要注意:

  • newpage開關從輸出實際的細胞For Each分離。這導致當前行被輸出,而不管newpage條件如何。
  • 某些變量(x,hy)必須在交換機內部以及外部都增加或修改,因此這些行現在已被複制。
  • rc的聲明已移至Do While循環之外。不需要爲每個單元格聲明它;而只是分配一個新的值。
  • 真的沒有必要多次檢查y + h > e.MarginBounds.Bottom(除非您打印真的很大),因爲您可以認爲如果您已經在新頁面的頂部,則只打印多行,所以它在循環結束時保持自己。
  • 當您評估Math.Max(h, rc.Height)時,h始終爲零或等於rc.Height,因此您可能會考慮重新考慮此問題。
+0

所以我應該在開始時設置newage = false,還是應該做其他事情? – PhpCoder

+0

@YiiFighter - 我編輯了我的答案。 'newpage'的值並不是問題,如果'newpage'爲True,那麼你正在放棄標題的當前行的標準輸出。 –

+0

謝謝你的朋友..你的修改解決了這個問題.... – PhpCoder