2016-06-13 3 views
0

데이터베이스에서 값을 가져 오는 datagridview을 인쇄하려고하는데 할 수 있었지만 인쇄 한 후 열 이름이 표시되지 않는 이유를 모르겠습니다.DataGridview 인쇄

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 
     With DataGridView3 
      Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit) 
      Dim text As String = ("TrashCollection - Estatísticas") 
      fmt.LineAlignment = StringAlignment.Center 
      fmt.Trimming = StringTrimming.EllipsisCharacter 
      e.Graphics.DrawString(text, New Font("Verdana", 10, FontStyle.Bold), Brushes.Black, 350, 30) 
      Dim y As Single = e.MarginBounds.Top 
      Do While mRow < DataGridView3.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(DataGridView3.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt) 
        Else 
         e.Graphics.DrawString(DataGridView3.Rows(cell.RowIndex).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 -= 0 
        newpage = True 
        Exit Sub 
       End If 
      Loop 
      mRow = 0 
     End With 
    End Sub 

그리고 인쇄 버튼에 나는이 코드 라인이 :이 사람이하는 PrintDocument입니다

: 아래는 제가 현재 사용하고있는 코드입니다 그래서

Private Sub cmdPrintDetailed_Click(sender As Object, e As EventArgs) Handles cmdPrintDetailed.Click 
     PrintDocument1.DefaultPageSettings.Landscape = True 
     PrintPreviewDialog1.Document = PrintDocument1 
     PrintPreviewDialog1.ShowDialog() 
    End Sub 

는, 기본적으로 행 대신 단순히 열 이름을 인쇄 할 수있게하고 싶습니다. 인쇄 미리보기에서 열이있는 datagridview을 모두 볼 수 있음을 거의 잊지 마십시오.

답변

0

이것은 내가 발견 한 가장 좋은 인쇄 예제이며 내 DataGrid 인쇄용으로 수정 한 것입니다. 이것은 열 머리글을 추가 할 때 도움이 될 수 있습니다. https://code.msdn.microsoft.com/VBNet-Printing-Example-bc3b0176

+0

안녕하세요! 도움을 주셔서 감사합니다,하지만이 데이터베이스의 데이터와 함께 작동합니까? –

+0

글쎄, 일단 데이터가 datagridview에 있으면 문제가 없어야합니다. 코드를 코드와 비교하십시오. 링크의 코드는 예를 들어 A4보다 넓고 A4보다 긴 코드도 인쇄합니다. 예제를로드하고 실행하십시오. – minimalist

+0

그 코드를 사용하려고 시도하는 동안'객체 참조가 인스턴스로 설정되지 않았습니다 '라는 Preview 버튼에 오류가 발생했습니다. 어떻게해야합니까? –