Impression datagridview

mohamed222222 Messages postés 161 Date d'inscription mardi 12 juin 2007 Statut Membre Dernière intervention 22 avril 2009 - 5 nov. 2008 à 10:30
cs_fauve Messages postés 661 Date d'inscription vendredi 2 décembre 2005 Statut Membre Dernière intervention 23 mars 2011 - 5 nov. 2008 à 18:20
Bonjour ;

j'utilise ce code pour imprimer un datagridview, il m'imprime bien ledatagrid view par contre je voudrai rajoute une date, un titre et le mettre en paysage dans ce code. Ou dois je le rajouter ?

merci beaucoup de votre aide !!!!!!

Private



Sub
ControlPrint_PrintPage(

ByVal
sender

As



Object
,

ByVal
e

As
PrintPageEventArgs)

Handles



MyBase
.PrintPage


'We get the old status of the control






Dim
NewSize

As



New
Size(width, height)


Dim
OldDock

As
DockStyle = m_ctrl.Dock


Dim
OldSize

As



New
Size(m_ctrl.Width, m_ctrl.Height)


Dim
parent

As
Control = m_ctrl


Dim
Parents

As
List(

Of
Control) =

New
List(

Of
Control)()


Dim
OldSizes

As
List(

Of
Size) =

New
List(

Of
Size)()


'enumerate the parents






Do



While
parent.Parent

IsNot



Nothing

Parents.Add(parent.Parent)


OldSizes.Add(parent.Parent.Size)


parent = parent.Parent







Loop






'Change the size of the control to fullt display it and get rid of scrollbars

m_ctrl.Dock = DockStyle.None


m_ctrl.Size = NewSize







'Make sure that the size changes otherwise resize the parents






Do



While
m_ctrl.Size = OldSize


For



Each
c

As
Control

In
Parentsc.Size =


New
Size(c.Width + 100, c.Height + 100)


Next
cm_ctrl.Size = NewSize


Loop






'print dimentions will be according to page size and margins






Dim
printwidth

As



Integer
= width


Dim
printheight

As



Integer
= height


'change the width to fit the paper, and change the height to maintain the ratio






If
printwidth > e.MarginBounds.Width

Then

printheight =




CInt
(Fix((

CSng
(e.MarginBounds.Width) /

CSng
(printwidth)) * printheight))printwidth = e.MarginBounds.Width


End



If






'if too long, we will need more papers






If
printheight - printedheight > e.MarginBounds.Height

AndAlso
(

Not
stretch)

Then

e.HasMorePages =




True






Else

e.HasMorePages =




False






End



If 








Dim
gp

As
GraphicsUnit = GraphicsUnit.Point


Dim
b

As



New
Bitmap(width, height)


'Good, now we can draw

m_ctrl.DrawToBitmap(b,




New
Rectangle(0, 0, width, height))


'Will we stretch the image?






If
stretch

Then

e.Graphics.DrawImage(b, e.MarginBounds, b.GetBounds(gp), gp)


e.HasMorePages =




False






Else






'If not stretched then make sure to print the area of the current page only






Dim
ScaleF

As



Single
=

CSng
(height) /

CSng
(printheight)printheight -= printedheight


If
printheight > e.MarginBounds.Height

Then

printheight = e.MarginBounds.Height







End



If






Dim
rect

As



New
Rectangle(0,

CInt
(Fix(printedheight * ScaleF)), b.Width,

CInt
(Fix(printheight * ScaleF)))


Dim
b2

As
Bitmap = b.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare)e.Graphics.DrawImage(b2,


New
Rectangle(

CInt
((e.PageBounds.Width / 2) - (printwidth \ 2)), e.MarginBounds.Top, printwidth, printheight))


End



If






If
e.HasMorePages

Then






'Change the printed height, and don't forget the RepeatArea

printedheight += e.MarginBounds.Height - inbetween







Else

printedheight = 0







End



If






'Restore the control's state






Dim
i

As



Integer
= 0


Do



While
i < Parents.CountParents(i).Size = OldSizes(i)

i += 1


Loop

m_ctrl.Size =




New
Size(OldSize.Width, OldSize.Height)m_ctrl.Dock = OldDock


End



Sub

1 réponse

cs_fauve Messages postés 661 Date d'inscription vendredi 2 décembre 2005 Statut Membre Dernière intervention 23 mars 2011 8
5 nov. 2008 à 18:20
Salut,

Il y a de çà quelques temps, j'ai crée une fonction permettant d'imprimer une ListView en passant par un fichier Excel.

Il reste plus qu'à changer la fonction et l'adapter à un DataGridView



Sub Lancer_impression(ByVal Listview As ListView)

        Dim S1 As String
        Dim FNxls As String
        Dim iCol As Integer
        Dim oXL As Object
        Dim oBook As Object
        Dim oSheet As Object
        Dim monProcess As New Process()

        S1 =Application.StartupPath    ' Retourne le chemin d'accèsde l'application
        If Mid(S1, Len(S1) - 1) <> "" Then S1 = S1 & ""
        FNxls = S1 & "Excel.xls"

        ' Creer l'application XL
        oXL = CreateObject("Excel.Application")
        oBook = oXL.Workbooks.Add ' Ajout d'un classeur
        oSheet = oBook.Worksheets(1)    ' Ajout d'une feuille

        With oSheet
           
    
        For i Asinteger = 0 To ListView.Columns.Count - 1 ' Permet
d'ajouter les nomsdes colonnes de la listview passer en paramètre dans
le fichier XL
                .Cells(1, i+1) = ListView.Columns(i).Text
            Next

            'Boucle ajoutant tout les items contenus dans la ListView dans lefichier excel
            For iCol = 0 To ListView.Items.Count - 1
                lvItem = ListView.Items(iCol)
               .Cells(iCol + 3, 1) = lvItem.SubItems(0).Text
               .Cells(iCol + 3, 2) = lvItem.SubItems(1).Text
               .Cells(iCol + 3, 3) = lvItem.SubItems(2).Text
               .Cells(iCol + 3, 4) = lvItem.SubItems(3).Text
               .Cells(iCol
+ 3, 4).HorizontalAlignment = 3 ' Permet de centrer dans lacellule ( 2
: à gauche      3 : centrer     4 : à droite )
               ' Ligne précédente peut être supprimé si pas de formatage voulu pourles cellules
               .Cells(iCol + 3, 5) = lvItem.SubItems(4).Text
               .Cells(iCol + 3, 5).HorizontalAlignment = 3
               .Cells(iCol + 3, 6) = lvItem.SubItems(5).Text
               .Cells(iCol + 3, 6).HorizontalAlignment = 3
               .Cells(iCol + 3, 7) = lvItem.SubItems(6).Text
               .Cells(iCol + 3, 7).HorizontalAlignment = 3
            Next
        End With

        ' 1° Ligne : Titres en Gras
        oSheet.Rows(1).Font.Bold = True

        ' Largeur des colonnes
        oSheet.Columns("A:A").ColumnWidth = 33.71   ' Colonne 1
        oSheet.Columns("B:B").ColumnWidth = 33.71   ' Colonne 2

        ' Paramétrer les marges de la feuille
        oSheet.PageSetup.LeftMargin= 0.393700787401575     ' Marge de gauche
        oSheet.PageSetup.RightMargin = 0.393700787401575    ' Marge de droite
        oSheet.PageSetup.TopMargin =0.393700787401575      ' Marge du haut
        oSheet.PageSetup.BottomMargin = 0.393700787401575   ' Marge du bas

        ' Afficher le quadrillage sur la feuille
        oSheet.PageSetup.PrintGridlines = True

        ' Afficher le tableau de manière centrer horizontalement sur la feuille
        oSheet.PageSetup.CenterHorizontally = True

        ' Paramétrer la feuille en mode Paysage        oSheet.PageSetup.Orientation 2  ' ( 1 : Portrait )

        oXL.ActiveSheet.printout() ' Lancer l'impression
        oXL.Quit() ' Ferme le processus

    End Sub

++

Winjet
0
Rejoignez-nous