2013-09-29 23 views
1

當數據網格視圖的所有行超出底部邊距時,將數據網格視圖的所有行打印到另一個頁面時,會出現問題。當我點擊打印預覽時,它會不停地添加頁面。這是我的代碼。在VS 2010上打印VB中的多個頁面

Dim mRow As Integer = 0 

    Dim newpage As Boolean = True 

    With dgvItems 

     Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit) 
     fmt.LineAlignment = StringAlignment.Center 
     fmt.Trimming = StringTrimming.EllipsisCharacter 
     Dim y As Single = a + 20 

     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, 70) 
       e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height) 

       If (newpage) Then 
        e.Graphics.DrawString(dgvItems.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt) 

       Else 
        e.Graphics.DrawString(dgvItems.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt) 

       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 

我也想請教一下如何設置VB的頁眉和頁腳在Visual Studio 2010中,這樣的頁眉和頁腳可以在所有頁面打印..

回答

1

希望這有助於:

Private index As Integer 
Private Sub Print(...) Handles PrintDocument1.PrintPage 
    Dim row As Integer = {some point you want to start at} 
    'Paint a title - since this event fires for each page 
    'continue loop or start loop 
    For i As Integer = index To myList.Count - 1 
    If Not row = e.MarginBounds.Bottom - 12 Then 
    'remember where we are in the list 
    index = i 
    'paint your contents 
    Else 
    'start new page 
    e.HasMorePages = True 
    Exit Sub 
    End If 
    Next 
    'reset the index for next print job 
    If Not e.HasMorePages Then index = 0 
End Sub