15. Sept 2019, 22:50
VB-fun.de - Beitragsübersicht zum VB.Net-Forum-Archiv 0066
  V05: datagridview drucken?
 Von  master_kunz
 Am  16. Juni 2007 um 20:20:48
 Frage Hallo zusamm

Kann man eine Datagirdview drucken? Oder gibt's ne alternative, z.b. in einer RTB?

master_kunz

PS: Das was ich brauche ist einfach ne druckfunktion (und ansicht) einer tabelle.

danke im vorraus
 Antwort:
 Von Klaus
 Am 17. Juni 2007 um 07:46:09
 Antwort Morjen,

was ist denn das schon wieder für eine Aussage??

Zitat:
DGV geht wohl nur indirekt zu drucken über Printform


Schon 'mal von PrintDocument gehört???
Thomas, etwas mehr Stil, bitte!!! (Dein eigenes Zitat)

Ein DataGridView auszudrucken ist eine individuelle Sache, also sollte man sich die Mühe machen, wenn man ernsthaft daran interessiert ist, auch eine sachgerechte Lösung zu finden.

Die Grundstruktur sieht folgend aus (auch wenn es wenige interessiert):
'Klaus D. Raudszus / VB 2005 / November 2006

Public Class clsDGVPrint

Private m_Pages As Integer
Private m_intTotalWidth As Integer
Private m_intPageHeight As Integer
Private m_intRowsToPrint As Integer
Private m_arColsWidth() As Integer
Private m_arHeaderText() As String
Private m_ftHeaderFont As Font
Private m_DataGridView As DataGridView
Private WithEvents m_PrintDoc As Printing.PrintDocument

Public Sub New()
Me.m_Pages = 0
Me.m_intTotalWidth = 0
Me.m_intPageHeight = 0
Me.m_intRowsToPrint = 0
Me.m_ftHeaderFont = New Font("Arial", 9.75!)
End Sub

Public Sub New(ByVal dgv As DataGridView)
Me.New()
Me.m_DataGridView = dgv
ReDim Me.m_arColsWidth(Me.m_DataGridView.ColumnCount - 1)
ReDim Me.m_arHeaderText(Me.m_DataGridView.ColumnCount - 1)
End Sub

Public Sub Print()
Me.m_PrintDoc = New Printing.PrintDocument
AddHandler m_PrintDoc.EndPrint, AddressOf Me.EndPrint
Dim intCols As Integer = Me.m_DataGridView.ColumnCount - 1
For i As Integer = 0 To intCols
Me.m_arColsWidth(i) = GetColumnsSize(i)
Me.m_arHeaderText(i) = GetHeaderText(i)
Me.m_intTotalWidth = Me.m_intTotalWidth + Me.m_arColsWidth(i)
Next
If Me.m_intTotalWidth > Me.m_PrintDoc.DefaultPageSettings.PaperSize.Width Then
Me.m_PrintDoc.DefaultPageSettings.Landscape = True
Me.m_PrintDoc.DefaultPageSettings.Margins.Top = 35
Me.m_intPageHeight = Me.m_PrintDoc.DefaultPageSettings.PaperSize.Width
Else
Me.m_intPageHeight = Me.m_PrintDoc.DefaultPageSettings.PaperSize.Height
End If
Dim ppd As PrintPreviewDialog = New PrintPreviewDialog()
ppd.Document = Me.m_PrintDoc
ppd.ShowDialog()
End Sub

Private Sub m_PrintDoc_PrintPage(ByVal sender As Object, _
ByVal e As Printing.PrintPageEventArgs) Handles m_PrintDoc.PrintPage
Me.PrintHeader(e.Graphics)
Me.PrintRows(e)
'Seitenzahl drucken
Dim str As String = "Seite " & Me.m_Pages.ToString
e.Graphics.DrawString(str, _
Me.m_DataGridView.Font, Brushes.Blue, _
Me.m_PrintDoc.DefaultPageSettings.Margins.Left, Me.m_intPageHeight - 75)
End Sub

Private Sub PrintHeader(ByVal g As Graphics)
Dim x As Integer = Me.m_PrintDoc.DefaultPageSettings.Margins.Left
Dim y As Integer = Me.m_PrintDoc.DefaultPageSettings.Margins.Top
For i As Integer = 0 To Me.m_arHeaderText.Length - 1
g.DrawString(Me.m_arHeaderText(i), Me.m_ftHeaderFont, Brushes.Blue, x, y)
x += Me.m_arColsWidth(i)
Next
End Sub

Private Sub PrintRows(ByVal e As Printing.PrintPageEventArgs)
Dim intCols As Integer = Me.m_DataGridView.ColumnCount - 1
Dim intRows As Integer = Me.m_DataGridView.RowCount - 2
Dim x As Integer = Me.m_PrintDoc.DefaultPageSettings.Margins.Left
Dim y As Integer = Me.m_PrintDoc.DefaultPageSettings.Margins.Top + 50
Me.m_Pages += 1
For i As Integer = Me.m_intRowsToPrint To intRows
For ii As Integer = 0 To intCols
Dim str As String = Me.m_DataGridView.Rows(i).Cells(ii).Value.ToString
e.Graphics.DrawString(str, Me.m_DataGridView.Font, Brushes.Black, x, y)
x += Me.m_arColsWidth(ii)
Next
x = Me.m_PrintDoc.DefaultPageSettings.Margins.Left
y += 30
If y > (Me.m_intPageHeight - _
Me.m_PrintDoc.DefaultPageSettings.Margins.Bottom) Then
e.HasMorePages = True
Me.m_intRowsToPrint = i + 1
Exit Sub
Else
e.HasMorePages = False
Me.m_intRowsToPrint = 0
End If
Next
End Sub

Private Function GetHeaderText(ByVal col As Integer) As String
Return Me.m_DataGridView.Columns(col).HeaderText
End Function

Private Function GetColumnsSize(ByVal col As Integer) As Integer
Dim sglWidth As Single = 0
Dim intRows As Integer = Me.m_DataGridView.RowCount - 2
Using g As Graphics = Graphics.FromHwnd(Me.m_DataGridView.Handle)
Dim newSizeF As SizeF = g.MeasureString(GetHeaderText(col), Me.m_ftHeaderFont)
sglWidth = newSizeF.Width + 15
For i As Integer = 0 To intRows
newSizeF = g.MeasureString( _
Me.m_DataGridView.Rows(i).Cells(col).Value.ToString, _
Me.m_DataGridView.Font)
If newSizeF.Width > sglWidth Then sglWidth = newSizeF.Width + 15
Next
End Using
Return CInt(sglWidth)
End Function

Private Sub EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
Me.m_Pages = 0
End Sub

End Class
@Master_Kunz oder wie auch immer, gewöhne dir einen einheitlichen Nicknamen an oder wechsele deine E-Mail, wenn du etwas zu verbergen hast.

Schönen Sonntag

Klaus
[ VB.Net-Forum | Archiv 0066 | Archiv-Übersicht ]
 Antworten
V05: datagridview drucken? - master_kunz 16. Juni 2007 um 20:20:48
Re: datagridview drucken? - Thomas 16. Juni 2007 um 21:14:03
Re: datagridview drucken? - Klaus 17. Juni 2007 um 07:46:09
Re: datagridview drucken? - Thomas 17. Juni 2007 um 14:03:02
Re: datagridview drucken? - Klaus 17. Juni 2007 um 15:52:42
Re: datagridview drucken? - Thomas 17. Juni 2007 um 16:52:22
Re: datagridview drucken? - Klaus 17. Juni 2007 um 17:32:01
Re: datagridview drucken? - Thomas 17. Juni 2007 um 17:55:25
THX - master_kunz 17. Juni 2007 um 21:04:34
Re: THX - Alex 27. Juni 2007 um 00:02:01
Re: THX - Klaus 27. Juni 2007 um 06:49:38

Zum Seitenanfang

Startseite | VB-/VBA-Tipps | Projekte | Tutorials | API-Referenz | Komponenten | Bücherecke | Gewinnspiele | VB-/VBA-Forum | DirectX | DirectX-Forum | VB.Net | VB.Net-Forum | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum