Bonjour,
bonjour à Tous,
sa fait longtemps que je cherche du code pour imprimer mon DataGridView complet avec visual basic 2010 express et j'ai testé tous les codes qu'ils s'affichent avec des recherches sur google mais en vain.
Mais finalement, j'ai trouver un code c# que j'ai converti en VB qui me convient parfaitement mais le seul problème c qu'il boucle infiniment ( boucle infini au niveau du nombre de pages )
NB: Le code ne génère aucune erreur
Voici mon code :
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
Dim iHeaderHeight As Double
'Set the left margin
Dim iLeftMargin As Integer = e.MarginBounds.Left
'Set the top margin
Dim iTopMargin As Integer = e.MarginBounds.Top
'Whether more pages have to print or not
Dim bMorePagesToPrint As Boolean = False
Dim iTmpWidth As Integer = 0
'For the first page to print set the cell width and header height
If bFirstPage Then
For Each GridCol As DataGridViewColumn In DataGridView_impot.Columns
iTmpWidth = CType(Math.Floor(CType((CType(GridCol.Width, Double) / (CType(iTotalWidth, Double) * (CType(iTotalWidth, Double) * (CType(e.MarginBounds.Width, Double) / CType(iTotalWidth, Double))))), Double)), Integer)
iHeaderHeight = (CType(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height, Integer) + 11)
' Save width and height of headers
arrColumnLefts.Add(iLeftMargin)
arrColumnWidths.Add(iTmpWidth)
iLeftMargin = (iLeftMargin + iTmpWidth)
Next
End If
'Loop till all the grid rows not get printed
Dim iRow As Double
While (iRow <= (DataGridView_impot.Rows.Count - 1))
Dim GridRow As DataGridViewRow = DataGridView_impot.Rows(iRow)
'Set the cell height
iCellHeight = (GridRow.Height + 5)
Dim iCount As Integer = 0
'Check whether the current page settings allows more rows to print
If (iTopMargin + (iCellHeight >= (e.MarginBounds.Height + e.MarginBounds.Top))) Then
bNewPage = True
bFirstPage = False
bMorePagesToPrint = True
Exit While
Else
If bNewPage Then
'Draw Header => raison sociale
e.Graphics.DrawString(Me.lbl_rs.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, (e.MarginBounds.Top - (e.Graphics.MeasureString(Me.lbl_rs.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)))
'Draw Date => date system
e.Graphics.DrawString(Me.lbl_date_now.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), Brushes.Black, (e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(Me.lbl_date_now.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), e.MarginBounds.Width).Width)), (e.MarginBounds.Top - (e.Graphics.MeasureString(Me.lbl_rs.Text, New Font(New Font(DataGridView_impot.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)))
'Draw Columns
iTopMargin = e.MarginBounds.Top
For Each GridCol As DataGridViewColumn In DataGridView_impot.Columns
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight), strFormat)
iCount = (iCount + 1)
Next
bNewPage = False
iTopMargin = (iTopMargin + iHeaderHeight)
End If
iCount = 0
'Draw Columns Contents
For Each Cel As DataGridViewCell In GridRow.Cells
If (Not (Cel.Value) Is Nothing) Then
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), CType(iTopMargin, Single), CType(arrColumnWidths(iCount), Integer), CType(iCellHeight, Single)), strFormat)
End If
'Drawing Cells Borders
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iCellHeight))
iCount = (iCount + 1)
Next
End If
iRow = (iRow + 1)
iTopMargin = (iTopMargin + iCellHeight)
End While
'If more lines exist, print another page.
If bMorePagesToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Catch exc As Exception
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Bonjour,
Mon problème est résolu concernant ma boucle infinie. Mais maintenant mon problème c que mon DataGridView est mal organisé dans l'impression; Toutes les colonnes s'écrivent dans une seule colonne de tel sorte que l'écriture est combinée.
Bonjour,
Merci pour la réponse mais j'ai déclaré toutes les variables et je n'ai aucune erreur, rien n'est souligné en rouge. En plus j'ai résolu mon premier problème de boucle infini ; c'était juste une parenthèse mal placé dans la conversion.
Mon pèsent problème est que le DGV est mal organisé dans l'impression
Oui, mais chez moi, pour tester les variables ne sont pas déclarées.
Je ne suis pas extralucide, donc pour trouver le bug, j'ai besoin de ces variables, sinon impossible de t'aider.
Les variables sont les mêmes dans ce code déclaré Public en haut de la class et je ne les utilise que dans cette partie du code
Public Shared arrColumnLefts As ArrayList = New ArrayList
Public Shared arrColumnWidths As ArrayList = New ArrayList
Public Shared iCellHeight As Double
Public Shared iCount As Double
Public Shared bFirstPage As Boolean
Public Shared bNewPage As Boolean
Public Shared iTotalWidth As Double
Public Shared strFormat As StringFormat
Private Sub bt_imprimer_tva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_imprimer_tva.Click
Dim r As String = CStr(MsgBox("Etes vous sûre de vouloir imprimer cette page ?", CType(vbQuestion + vbYesNo +
vbDefaultButton2, MsgBoxStyle)))
If CDbl(r) <> vbYes Then Exit Sub
'paramétres d'impression
PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.DefaultPageSettings.Landscape = False ' orientation portrait
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
End If
End Sub
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
Try
strFormat = New StringFormat
strFormat.Alignment = StringAlignment.Near
strFormat.LineAlignment = StringAlignment.Center
strFormat.Trimming = StringTrimming.EllipsisCharacter
arrColumnLefts.Clear()
arrColumnWidths.Clear()
iCellHeight = 0
iCount = 0
bFirstPage = True
bNewPage = True
' Calculating Total Widths
iTotalWidth = 0
For Each dgvGridCol As DataGridViewColumn In DataGridView_impot.Columns
iTotalWidth = (iTotalWidth + dgvGridCol.Width)
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
Dim iHeaderHeight As Double
'Set the left margin
Dim iLeftMargin As Integer = e.MarginBounds.Left
'Set the top margin
Dim iTopMargin As Integer = e.MarginBounds.Top
'Whether more pages have to print or not
Dim bMorePagesToPrint As Boolean = False
Dim iTmpWidth As Integer = 0
'For the first page to print set the cell width and header height
If bFirstPage Then
For Each GridCol As DataGridViewColumn In DataGridView_impot.Columns
iTmpWidth = CType(Math.Floor(CType((CType(GridCol.Width, Double) / (CType(iTotalWidth, Double) * (CType(iTotalWidth, Double) * (CType(e.MarginBounds.Width, Double) / CType(iTotalWidth, Double))))), Double)), Integer)
iHeaderHeight = (CType(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height, Integer) + 11)
' Save width and height of headers
arrColumnLefts.Add(iLeftMargin)
arrColumnWidths.Add(iTmpWidth)
iLeftMargin = (iLeftMargin + iTmpWidth)
Next
End If
'Loop till all the grid rows not get printed
Dim iRow As Integer = 0
While (iRow <= (DataGridView_impot.Rows.Count - 1))
Dim GridRow As DataGridViewRow = DataGridView_impot.Rows(iRow)
'Set the cell height
iCellHeight = (GridRow.Height + 5)
Dim iCount As Integer = 0
'Check whether the current page settings allows more rows to print
If (iTopMargin + iCellHeight >= (e.MarginBounds.Height + e.MarginBounds.Top)) Then
bNewPage = True
bFirstPage = False
bMorePagesToPrint = True
Exit While
Else
If bNewPage Then
'Draw Header => raison sociale
e.Graphics.DrawString(Me.lbl_rs.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, (e.MarginBounds.Top - (e.Graphics.MeasureString(Me.lbl_rs.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)))
'Draw Date => date system
e.Graphics.DrawString(Me.lbl_date_now.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), Brushes.Black, (e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(Me.lbl_date_now.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), e.MarginBounds.Width).Width)), (e.MarginBounds.Top - (e.Graphics.MeasureString(Me.lbl_rs.Text, New Font(New Font(DataGridView_impot.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)))
'Draw Columns
iTopMargin = e.MarginBounds.Top
For Each GridCol As DataGridViewColumn In DataGridView_impot.Columns
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), CInt(iHeaderHeight)))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), CInt(iHeaderHeight)))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), CSng(iHeaderHeight)), strFormat)
iCount = (iCount + 1)
Next
bNewPage = False
iTopMargin = CInt(iTopMargin + iHeaderHeight)
End If
iCount = 0
'Draw Columns Contents
For Each Cel As DataGridViewCell In GridRow.Cells
If (Not (Cel.Value) Is Nothing) Then
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), CType(iTopMargin, Single), CType(arrColumnWidths(iCount), Integer), CType(iCellHeight, Single)), strFormat)
End If
'Drawing Cells Borders
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), CInt(iCellHeight)))
iCount = (iCount + 1)
Next
End If
iRow = iRow + 1
iTopMargin = CInt(iTopMargin + iCellHeight)
End While
'If more lines exist, print another page.
If bMorePagesToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Catch exc As Exception
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Dim r As String = CStr(MsgBox("Etes vous sûre de vouloir imprimer cette page ?", CType(vbQuestion + vbYesNo +
vbDefaultButton2, MsgBoxStyle)))
If CDbl(r) <> vbYes Then Exit Sub
à remplacer par
If (MessageBox.Show("Etes vous sûr de vouloir imprimer cette page ?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No) Then Exit Sub
alors que son homologue C#, avec les variables identiques retourne 209, 411, etc.... (d'ou l'intérêt de connaitre le contenu.....)
Voici une traduction "brute" qui retourne la même chose que le code C#
26 janv. 2016 à 16:43
26 janv. 2016 à 16:56
Mon problème est résolu concernant ma boucle infinie. Mais maintenant mon problème c que mon DataGridView est mal organisé dans l'impression; Toutes les colonnes s'écrivent dans une seule colonne de tel sorte que l'écriture est combinée.
26 janv. 2016 à 18:59
On veut bien essayer de dépanner, mais pas faire de la voyance....
Si tu copies ce code dans un projet neuf, regarde tout ce qui se souligne en rouge, et reviens ici le décrire.
27 janv. 2016 à 09:19
Merci pour la réponse mais j'ai déclaré toutes les variables et je n'ai aucune erreur, rien n'est souligné en rouge. En plus j'ai résolu mon premier problème de boucle infini ; c'était juste une parenthèse mal placé dans la conversion.
Mon pèsent problème est que le DGV est mal organisé dans l'impression
Modifié par Whismeril le 27/01/2016 à 09:37
Je ne suis pas extralucide, donc pour trouver le bug, j'ai besoin de ces variables, sinon impossible de t'aider.