2016-12-12 8 views
0

システムに印刷プレビューダイアログコントロールがあり、アプリケーションにあるDGVテーブルを印刷したい。私は問題なくテーブルを印刷できますが、非常に不思議な問題があります。印刷プレビューダイアログを閉じるとDGVヘッダーが消える

アプリケーションを実行して印刷プレビューコントロールを開いた後、thisと表示されます。印刷プレビューダイアログを閉じて、システム内で干渉した後に再び開くと、thisが得られます。誰がなぜこれが起こっているのか知っていますか?

これは私が使用しているコードです。

Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit) 
    fmt.LineAlignment = StringAlignment.Center 
    fmt.Trimming = StringTrimming.EllipsisCharacter 
    Dim y As Single = e.MarginBounds.Top 
    Dim rc As Rectangle 
    Dim x As Int32 
    Dim h As Int32 = 0 

    Do While mRow < dgvChemical.RowCount 
     Dim row As DataGridViewRow = dgvChemical.Rows(mRow) 
     x = e.MarginBounds.Left 
     h = 0 

     If newPage Then 
      For Each cell As DataGridViewCell In row.Cells 
       rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height) 
       e.Graphics.FillRectangle(Brushes.LightGray, rc) 
       e.Graphics.DrawRectangle(Pens.Black, rc) 
       e.Graphics.DrawString(dgvChemical.Columns(cell.ColumnIndex).HeaderText, dgvChemical.Font, Brushes.Black, rc, fmt) 
       x += rc.Width 
       h = Math.Max(h, rc.Height) 
      Next 
      y += h 
      mRow += 0 
     End If 
     newPage = False 

     x = e.MarginBounds.Left 

     For Each cell As DataGridViewCell In row.Cells 
      rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height) 
      e.Graphics.DrawRectangle(Pens.Black, rc) 
      e.Graphics.DrawString(cell.FormattedValue.ToString(), dgvChemical.Font, Brushes.Black, rc, fmt) 

      x += rc.Width 
      h = Math.Max(h, rc.Height) 
     Next 

     y += h 
     mRow += 1 
     If y + h > e.MarginBounds.Bottom Then 
      e.HasMorePages = True 
      newPage = True 
      Return 
     End If 
    Loop 

    mRow = 0 

    Dim ps As PaperSize 
    For ix As Integer = 0 To PrintDocument1.PrinterSettings.PaperSizes.Count - 1 
     If PrintDocument1.PrinterSettings.PaperSizes(ix).Kind = PaperKind.A3 Then 
      ps = PrintDocument1.PrinterSettings.PaperSizes(ix) 
      PrintDocument1.DefaultPageSettings.PaperSize = ps 
      PageSetupDialog1.PageSettings.PaperSize = ps 
     End If 
    Next` 
+0

「newPage」はリセットされていない可能性があります。これはどこに宣言されていますか?もう一度印刷プレビューに来るときは、これを 'True'に戻してみてください。このコードを何と呼びますか? – Bugs

答えて

0

answer to your previous postmRownewpageが印刷を開始するボタンクリックでをリセットしていることを確認しに箇条書きが含まれています。さもなければ、これらのフォーム/クラス・レベル変数は、最後のセッションからの値で始まります。

スタートページをリセットすることもできます。ユーザーがページ17を表示したままにしておくと、次回にそのページでも開始されます。それは望ましくないかもしれません。

Private Sub printDGV_Click(sender As Object, e As EventArgs) Handles printDGV.Click 
    ' need to start fresh eash time 
    mRow = 0 
    newpage = True 

    PrintPreviewDialog1.Document = PrintDocument1 
    ' optionally reset the first page shown 
    PrintPreviewDialog1.PrintPreviewControl.StartPage = 0 
    PrintPreviewDialog1.ShowDialog() 

End Sub 
関連する問題