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
을 모두 볼 수 있음을 거의 잊지 마십시오.
안녕하세요! 도움을 주셔서 감사합니다,하지만이 데이터베이스의 데이터와 함께 작동합니까? –
글쎄, 일단 데이터가 datagridview에 있으면 문제가 없어야합니다. 코드를 코드와 비교하십시오. 링크의 코드는 예를 들어 A4보다 넓고 A4보다 긴 코드도 인쇄합니다. 예제를로드하고 실행하십시오. – minimalist
그 코드를 사용하려고 시도하는 동안'객체 참조가 인스턴스로 설정되지 않았습니다 '라는 Preview 버튼에 오류가 발생했습니다. 어떻게해야합니까? –