Bonjour,
C'est la conversion d'une classe C# DataGridViewPrinter, voir
http://www.codeproject.com/csharp/datagridviewprinter.asp
En VB .NET, il ya un pb de gestion de l'impression d'une datagridview. La seule DLL disponible étant de rustemsoft étant payante.
DataGridViewPrinter : est une classe pour imprimer les datagridview. Un example est fourni avec.
Bonne programmation à tous,
Riadh.
Source / Exemple :
voir le fichier zip
25 mars 2008 à 21:33
@++ et bonne prog.
Vive l'opensource!!
3 avril 2008 à 14:51
9 avril 2008 à 11:10
Super classe, mais j'ai apporter quelques modifications pour gérer les images (colonnes de type Byte() ) :
Dans la procédure :
DrawRows :
' Printing the cell text
g.DrawString(TheDataGridView.Rows(CurrentRow).Cells(CurrentCell).EditedFormattedValue.ToString(), RowFont, RowForeBrush, CellBounds, CellFormat)
remplacée par :
' Printing the cell text
'Modification NH : gestion des images
If TypeOf TheDataGridView.Rows(CurrentRow).Cells(CurrentCell).Value Is Byte() Then
Dim lLocation As Point = New Point(CInt(CellBounds.Location.X), CInt(CellBounds.Location.Y))
g.DrawImageUnscaled(Bitmap.FromStream(ByteArrayToStream(DirectCast(TheDataGridView.Rows(CurrentRow).Cells(CurrentCell).Value, Byte()))), lLocation)
Else
g.DrawString(TheDataGridView.Rows(CurrentRow).Cells(CurrentCell).EditedFormattedValue.ToString(), RowFont, RowForeBrush, CellBounds, CellFormat)
End If
Note : code de la fonction ByteArrayToStream :
''' <summary>
''' Transforme un tableau d'octet en flux
''' </summary>
''' Tableau à convertir
''' <returns></returns>
''' <remarks></remarks>
Public Function ByteArrayToStream(ByRef pBytes As Byte()) As IO.Stream
Dim lMem As New IO.MemoryStream
lMem.Write(pBytes, 0, pBytes.Length)
Return lMem
End Function
Et dans Calculate :
tmpSize = g.MeasureString(TheDataGridView.Rows(j).Cells(i).EditedFormattedValue.ToString(), tmpFont)
remplacé par :
'Modification NH : gestion des images
If TypeOf TheDataGridView.Rows(j).Cells(i).Value Is Byte() Then
tmpSize = Bitmap.FromStream(ByteArrayToStream(DirectCast(TheDataGridView.Rows(j).Cells(i).Value, Byte()))).Size
Else
tmpSize = g.MeasureString(TheDataGridView.Rows(j).Cells(i).EditedFormattedValue.ToString(), tmpFont)
End If
Sinon, j'ai une question, que faut-il modifier pour forcer l'impression sur une seule page en largeur (comme dans Excel) ?
Je note rarement les sources, mais là je fais une exception (9/10, car ne gérait pas les images et pas trouvé la possibilité de mettre sur une seule page).
28 juil. 2008 à 13:11
28 oct. 2008 à 11:58
très bonne source, très utile dans mon cas.
j'ai juste du bidouiller car je voulais que les couleurs affichées de la grille s'impriment.
j'ai donc ajouter une jambe de bois:
dans la fonction drawrows:
j'ai ajouté juste après les définitions de variables:
Dim PrintRowColors As Boolean
If MsgBox("Imprimer les couleurs affichées de la grille ?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
PrintRowColors = True
Else
PrintRowColors = False
End If
j'ai remplacé
' Filling the back of the CurrentRow
If CurrentRow Mod 2 = 0 Then
g.FillRectangle(RowBackBrush, RowBounds)
Else
g.FillRectangle(RowAlternatingBackBrush, RowBounds)
End If
par
' Filling the back of the CurrentRow
If PrintRowColors = False Then
If CurrentRow Mod 2 = 0 Then
g.FillRectangle(RowBackBrush, RowBounds)
Else
g.FillRectangle(RowAlternatingBackBrush, RowBounds)
End If
End If
et enfin j'ai remplacé:
ColumnWidth = ColumnsWidth(CurrentCell)
Dim CellBounds As New RectangleF(CurrentX, CurrentY, ColumnWidth, RowsHeight(CurrentRow))
' Printing the cell text
par
ColumnWidth = ColumnsWidth(CurrentCell)
Dim CellBounds As New RectangleF(CurrentX, CurrentY, ColumnWidth, RowsHeight(CurrentRow))
If PrintRowColors = True Then
' printing the cell backcolor
g.FillRectangle(New SolidBrush(TheDataGridView.Rows(CurrentRow).Cells(CurrentCell).Style.BackColor), CellBounds)
End If
' Printing the cell text
ainsi, le prg va vous demander si vous voulez imprimer les couleurs de lignes alternées, ou les couleurs par cellules que vous auriez définies par programmation.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.