IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

imprimer un datagrid view [Débutant]


Sujet :

VB.NET

  1. #1
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut imprimer un datagrid view
    Bonjour à tous!
    alors, voici mon problème:je cherche à imprimer un datagrid view,
    j'ai essayé la méthode dans le tuto de plasserre mais cela n'imprime pas correctement: j'ai une toute partie du datagrid et sans voir les traits des colonnes ni leur contenu ni leur en-tête....
    quelqu'un aurait-il connaissance d'une méthode ou d'un tuto qui ferait mon bonheur?

    j'aimerai pouvoir imprimer la partie visible de mon datagrid en entier (les colonnes que je ne veux pas imprimer sont déjà cachées...

    merci d'avance à tous!

  2. #2
    Membre expérimenté Avatar de hunteshiva
    Homme Profil pro
    Chef de projet en SSII
    Inscrit en
    Février 2010
    Messages
    1 069
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Chef de projet en SSII
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2010
    Messages : 1 069
    Points : 1 455
    Points
    1 455
    Par défaut
    Bonjours,

    essaye avec ça
    & "imprimer datagridveiw vb.net"

    tu trouve vite des solutions a ton probleme:

  3. #3
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    merci mais google, je suis pas idiot, c le premier truc que j'ai cherché...
    je vais regarder tes liens...




    pour le premier lien, les liens dedans sont morts
    et le code est le code qui est dans le tuto de plasserre, le code qui ne va pas (ou que je n'arrive pas à paramètrer...

  4. #4
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

  5. #5
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    je l'avais trouvé celui là mais je ne sais pas comment le mettre dans mon soft vb.net (je suis débutant)
    car ce n'est pas du vb.net... c'est de asp ou du c d'ailleurs?du c

  6. #6
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    Citation Envoyé par Graffito Voir le message
    pourrais tu m'indiquer une technique pour le faire fonctionner en vb.net stp?
    j'ai essayé en traduisant mais ça marche pas...

  7. #7
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    pourrais tu m'indiquer une technique pour le faire fonctionner en vb.net stp?
    regarde cette discussion : http://www.developpez.net/forums/d11...er-code-cs-vb/
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

  8. #8
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    bon j'ai finalement réussi à mettre les 2 projets dans la solution et à référencer le c dans le vb, mais je n'arrive pas à trouver comment lancer l'impression à partir de vb...

    j'ai essayé avec ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
      Public declare Sub btnPrint_Click Lib "DataGridViewPrinterApplication" Alias btnPrint_Click(object sender, EventArgs e)handles button4.click
    mais il me dit qu'il manque la constante de chaine...

    par contre, je n'ai pas trouvé la DLL. j'ai rajouté la référence en la prenant dans projets mais je ne trouve pas la dll sur mon disque dur...

  9. #9
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut imprimer datagridview en vb.net
    bonjour wizard
    voici un code vb.net simple pour imprimer un dgv meme s'il contient des combobox,checkboxes,et images....à mettre dans dans ton forme.....

    le code comporte 2 forms:
    -le Form1 principal :1er bouton pour filler le datagridview ,2eme pour l'imprimer + un dgv + 1 PrintDocument+ 1 PrintPreviewDialog.
    -le Form2 pour raison de commodite contient seulement un controle PrintPreviewControl destine à l'affichage de l'apercu.....
    C'est en vb.net pret à l'emploi qui je l'espere fera ton bonheur....

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
     
    'NB:pour l'exemple j'ai pris les donnees du tableau 
    'des cultures/pays:CultureInfo.GetCultures pour mettre des donnees 
    'sur le datagridview "Cobaye"
     
    Imports System.Threading
    Imports System.Globalization
    Imports System.Windows.Forms
    Public Class Form1
    	Dim myForm As Form2 = New Form2
    	Public Sub New()
     
    		' Cet appel est requis par le Concepteur Windows Form.
    		InitializeComponent()
     
    		' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
    		btnPrintDGV.Enabled = False
    	End Sub
     
    	Private Sub btnFillDGV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillDGV.Click
    		SetupDataGridView()
    		btnPrintDGV.Enabled = True
    	End Sub
    	Private Sub btnPrintDGV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintDGV.Click
    		'1/uncomment pour envoyer directement à l'imprimante à l'imprimante
    		Me.PrintDocument1.Print()
    		'
    		'
    		'
    		'2/uncomment pour afficher sur le control PrintPreviewDialog de ce Form
    		'(apercu)
    		'Me.PrintPreviewDialog1.Document = PrintDocument1
    		'Me.PrintPreviewDialog1.Show()
    		'
    		'
    		'3/uncomment pour afficher sur un controle PrintPreviewDialog
    		'place sur le Form2
    		'Me.myForm.PrintPreviewControl1.AutoZoom = True
    		'Me.myForm.PrintPreviewControl1.Document = PrintDocument1
    		'Me.myForm.Show()
    	End Sub
    	'Fill du DataGridView
    	Private Sub SetupDataGridView()
    		'AJOUT DES VALEURS POMPEES DE  CultureInfo(gratis) à MyListCI
    		'Clear the DataGridViewLanguages control
    		DataGridView1.DataSource = Nothing
     
     
    		'Presentation Colonnes Lignes:En-tete non visible
    		Me.DataGridView1.RowHeadersVisible = False
    		' Disable editing features.
    		DataGridView1.AllowUserToAddRows = False
    		DataGridView1.AllowUserToDeleteRows = False
    		DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically
    		'Presentation Colonnes
    		DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
    		DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    		DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.WhiteSmoke
    		DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue
    		DataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
     
    		'Auto-generation des colonnes moins colonnes NumberFormat & DateTimeFormat
    		'avec Remove on peut exclure certaines colonnes de la generation
    		'ici:ces 2 colonnes generes une exception en realtion avec culture -ar- 
    		'c'est une autre facon de personnaliser au passage moins souple.
    		With Me.DataGridView1
    			.AutoGenerateColumns = True
    			.DataSource = CultureInfo.GetCultures(CultureTypes.AllCultures)
    			.Columns.Remove("NumberFormat")
    			.Columns.Remove("DateTimeFormat")
    		End With
    		Me.btnPrintDGV.Enabled = True
    	End Sub
    	'variables pour impression
    	'ne t'occupe des Combox,CheckBoxes et Images 
    	'tu peux sans essayer sans eux.
    	'Simplement l'auteur du code qui n'est pas moi a prevu de gerer meme ces cas......
    	Private oStringFormat As StringFormat
    	Private oStringFormatComboBox As StringFormat
    	Private oButton As Button
    	Private oCheckbox As CheckBox
    	Private oComboBox As ComboBox
     
    	Private nTotalWidth As Int16
    	Private nRowPos As Int16
    	Private NewPage As Boolean
    	Private nPageNo As Int16
    	Private Header As String = "Header Test"
    	Private sUserName As String = "Will"
    	Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
     
    		oStringFormat = New StringFormat
    		oStringFormat.Alignment = StringAlignment.Near
    		oStringFormat.LineAlignment = StringAlignment.Center
    		oStringFormat.Trimming = StringTrimming.EllipsisCharacter
     
    		oStringFormatComboBox = New StringFormat
    		oStringFormatComboBox.LineAlignment = StringAlignment.Center
    		oStringFormatComboBox.FormatFlags = StringFormatFlags.NoWrap
    		oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
     
    		oButton = New Button
    		oCheckbox = New CheckBox
    		oComboBox = New ComboBox
     
    		nTotalWidth = 0
    		For Each oColumn As DataGridViewColumn In DataGridView1.Columns
     
    			nTotalWidth += oColumn.Width
     
    		Next
    		nPageNo = 1
    		NewPage = True
    		nRowPos = 0
     
    	End Sub
     
    	Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
     
    		Static oColumnLefts As New ArrayList
    		Static oColumnWidths As New ArrayList
    		Static oColumnTypes As New ArrayList
    		Static nHeight As Int16
     
    		Dim nWidth, i, nRowsPerPage As Int16
    		Dim nTop As Int16 = e.MarginBounds.Top
    		Dim nLeft As Int16 = e.MarginBounds.Left
     
    		If nPageNo = 1 Then
     
    			For Each oColumn As DataGridViewColumn In DataGridView1.Columns
     
    				nWidth = CType(Math.Floor(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)
     
    				nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 11
     
    				oColumnLefts.Add(nLeft)
    				oColumnWidths.Add(nWidth)
    				oColumnTypes.Add(oColumn.GetType)
    				nLeft += nWidth
     
    			Next
     
    		End If
     
    		Do While nRowPos < DataGridView1.Rows.Count - 1
     
    			Dim oRow As DataGridViewRow = DataGridView1.Rows(nRowPos)
     
    			If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
     
    				DrawFooter(e, nRowsPerPage)
     
    				NewPage = True
    				nPageNo += 1
    				e.HasMorePages = True
    				Exit Sub
     
    			Else
     
    				If NewPage Then
     
    					' Draw Header
    					e.Graphics.DrawString(Header, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
     
    					' Draw Columns
    					nTop = e.MarginBounds.Top
    					i = 0
    					For Each oColumn As DataGridViewColumn In DataGridView1.Columns
     
    						e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
    						e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
    						e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
    						i += 1
     
    					Next
    					NewPage = False
     
    				End If
     
    				nTop += nHeight
    				i = 0
    				For Each oCell As DataGridViewCell In oRow.Cells
     
    					If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
     
    						e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
     
    					ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
     
    						oButton.Text = oCell.Value.ToString
    						oButton.Size = New Size(oColumnWidths(i), nHeight)
    						Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
    						oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
    						e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
     
    					ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
     
    						oCheckbox.Size = New Size(14, 14)
    						oCheckbox.Checked = CType(oCell.Value, Boolean)
    						Dim oBitmap As New Bitmap(oColumnWidths(i), nHeight)
    						Dim oTempGraphics As Graphics = Graphics.FromImage(oBitmap)
    						oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
    						oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width - oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) / 2, Int32), oCheckbox.Width, oCheckbox.Height))
    						e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
     
    					ElseIf oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
     
    						oComboBox.Size = New Size(oColumnWidths(i), nHeight)
    						Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
    						oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
    						e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
    						e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight), oStringFormatComboBox)
     
    					ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn) Then
     
    						Dim oCellSize As Rectangle = New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)
    						Dim oImageSize As Size = CType(oCell.Value, Image).Size
    						e.Graphics.DrawImage(oCell.Value, New Rectangle(oColumnLefts(i) + CType(((oCellSize.Width - oImageSize.Width) / 2), Int32), nTop + CType(((oCellSize.Height - oImageSize.Height) / 2), Int32), CType(oCell.Value, Image).Width, CType(oCell.Value, Image).Height))
     
    					End If
     
    					e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
     
    					i += 1
     
    				Next
     
    			End If
     
    			nRowPos += 1
    			nRowsPerPage += 1
     
    		Loop
     
    		DrawFooter(e, nRowsPerPage)
     
    		e.HasMorePages = False
     
    	End Sub
     
    	Private Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
     
    		Dim sPageNo As String = nPageNo.ToString + " of " + Math.Ceiling(DataGridView1.Rows.Count / RowsPerPage).ToString
     
    		' Right Align - User Name
    		e.Graphics.DrawString(sUserName, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 7)
     
    		' Left Align - Date/Time
    		e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height + 7)
     
    		' Center  - Page No. Info
    		e.Graphics.DrawString(sPageNo, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31)
     
    	End Sub
    End Class
     
     
    Public Class Form2
    	Public Sub New()
     
    		' Cet appel est requis par le Concepteur Windows Form.
    		InitializeComponent()
     
    		' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
     
    	End Sub
     
     
     
    End Class
    bon code.................

  10. #10
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    j'ai une erreur à:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
    il me disait erreur nullobject........ faites new ......
    (je voulais relancer mon appli pour mettre l'erreur en question et maintenant, cela la fait planter... (complètement, même si je fais stop, les form ne se ferment plus...))

    bon après avoir relancé mon pc, l'erreur est:


    ce qui est bizarre, c'est que en faisant une appli pour tester le code ça fonctionnait... est ce que le problème ne viendrait pas du fait que mon datagridview est lié à un data set? j'ai lu que ca posait problème...

  11. #11
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut impremer un datagrid
    bonjour wizard
    Tres bizarre mais le message d'erreur est explicite .La variable Ostring n'est pas initialisee.
    Elle doit etre dans une porte accessible à PrintDocument_PrintPage.....c.à.d au niveau du Form qui le contient......
    D'autre part la sub PrintDocument_BeginPrint est absolument necessaire car cet evenement est declenche avant PrintDocument_PrintPage .
    C'est la ou l'on profite pour initialiser nos variables globales y compris Ostring.....
    Ce qui me fait penser que tu as reorganise le code......................
    bon code............

  12. #12
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    tout ce que j'ai fait c'est de nommer correctement le dgv...
    je ne me suis pas du tout amusé à réorganiser le code...

  13. #13
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut imprimer dgv
    bonjour wizard
    Bah essaye de mettre un 1er BreakPoint pour voir si tu rentre dans la sub d'initialisation de PrintDocument
    code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
     
    		'breakpoint point ici parce qu'il plante sur ce oStringFormat
    		'd'apres ton post....cette variable gere les cellules contenant du Text et ou des hyperlien
    		oStringFormat = New StringFormat
    		oStringFormat.Alignment = StringAlignment.Near
    		oStringFormat.LineAlignment = StringAlignment.Center
    		oStringFormat.Trimming = StringTrimming.EllipsisCharacter
    Si le deboggueur ne passe à cette ligne c'est un probleme....
    Dans ce cas fais l'essai suivant :imprimer sans le formatage prevu par la classe StringFormat .Cela est possible car c'est un argument optionnel de formatage de e.Graphics.DrawString
    Fais le en mode deboggage pour voir si vraiment le probleme est cause par StringFormat ou autre chose.....
    En posant un breakpoint sur la ligne que tu as poste..........
    comme ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
         'breakpoint ici apres avoir enleve l'argument facultatif oString 
         If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
     
    						e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
    Maintenant pour information j'ai teste l'impression d'un dgv lie à un dataset et cela ne pose aucun probleme.........

  14. #14
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    bonjour mabrouki,
    j'ai mis un point d'arrêt sur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    oStringFormat = New StringFormat
    ,
    le programme y va (elle est surlignée en jaune)
    en fait (j'imprime en pdf) cela fonctionne jusqu'a 75% de la progress bar d'impression en pdf et à ce moment, j'ai l'erreur citée plus haut...
    (en mettant mon imprimante au lieu du pdf, même erreur au même moment mais sans l'affichage de la progressbar...)

    merci de m'aider!

  15. #15
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    lorsque je créée un projet séparé pour tester, je n'ai pas l'erreur mais je n'ai rien dans l'impression, sauf la date, n° de page et "will":


    franchement, si quelqu'un peut m'aider, cela m'évitera de m'arracher tous les cheveux (y en reste plus beaucoup depuis le temps que je bloque sur cette p***** d'impression...)

  16. #16
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour ozthewizard
    Est ce que le probleme ne serait pas du au driver pdf utilise.
    En tous cas chez moi j'utilise le driver pdf d'acrobat 7 pro.
    Je te donne ci-dessous le code vb precedent avec un datataset,un datatable.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
     
     
    l'impression est faite avec le driver d'Acrobat 7 pro installe sur ma machine
    Imports System.Threading
    Imports System.Globalization
    Imports System.Windows.Forms
    Public Class frmPrintDGV
    	Dim myForm As Form2 = New Form2
     
    	Public Sub New()
     
    		' Cet appel est requis par le Concepteur Windows Form.
    		InitializeComponent()
     
    		' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
    		btnPrintDGV.Enabled = False
    	End Sub
     
    	Private Sub btnFillDGV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillDGV.Click
    		SetupDataGridView()
    		btnPrintDGV.Enabled = True
    	End Sub
     
    	'Fill du DataGridView
    	Private Sub SetupDataGridView()
    		'Le datasource PersonnelDS et la table Personnel
    		'Clear the DataGridViewLanguages control
    		DataGridView1.DataSource = Nothing
    		Me.Personnel1TableAdapter1.Fill(Me.PersonnelDS1.Personnel1)
    		DataGridView1.DataSource = Me.PersonnelDS1
    		DataGridView1.DataMember = Me.PersonnelDS1.Personnel1.TableName
     
     
    		Me.btnPrintDGV.Enabled = True
    	End Sub
    	Private Sub btnPrintDGV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintDGV.Click
    		'1 envoyer directement à l'imprimante à PDF
    		'sinon utilise imprimante Microsoft Office Document Image Writer en format Tiff
    		'que tu peux convertir en PDF
    		Me.PrintDocument1.Print()
    		'
    		'
    		'
    		'2/ affiche l'apercu  sur le control PrintPreviewDialog de ce Form
    		'
    		'Me.PrintPreviewDialog1.Document = PrintDocument1
    		'Me.PrintPreviewDialog1.Show()
    		'
    		'
    		'3/uncomment pour afficher sur un controle PrintPreviewDialog
    		'place sur le Form2
    		'Me.myForm.PrintPreviewControl1.AutoZoom = True
    		'Me.myForm.PrintPreviewControl1.Document = PrintDocument1
    		'Me.myForm.Show()
    	End Sub
    	'variables pour impression
    	'ne t'occupe des Combox,CheckBoxes et Images 
    	'tu peux sans essayer sans eux.
    	'Simplement l'auteur du code qui n'est pas moi a prevu de gerer meme ces cas......
    	Private oStringFormat As StringFormat
    	Private oStringFormatComboBox As StringFormat
    	Private oButton As Button
    	Private oCheckbox As CheckBox
    	Private oComboBox As ComboBox
     
    	Private nTotalWidth As Int16
    	Private nRowPos As Int16
    	Private NewPage As Boolean
    	Private nPageNo As Int16
    	Private Header As String = "Header Test"
    	Private sUserName As String = "Will"
    	Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
     
    		oStringFormat = New StringFormat
    		oStringFormat.Alignment = StringAlignment.Near
    		oStringFormat.LineAlignment = StringAlignment.Center
    		oStringFormat.Trimming = StringTrimming.EllipsisCharacter
     
    		oStringFormatComboBox = New StringFormat
    		oStringFormatComboBox.LineAlignment = StringAlignment.Center
    		oStringFormatComboBox.FormatFlags = StringFormatFlags.NoWrap
    		oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
     
    		oButton = New Button
    		oCheckbox = New CheckBox
    		oComboBox = New ComboBox
     
    		nTotalWidth = 0
    		For Each oColumn As DataGridViewColumn In DataGridView1.Columns
     
    			nTotalWidth += oColumn.Width
     
    		Next
    		nPageNo = 1
    		NewPage = True
    		nRowPos = 0
     
    	End Sub
     
    	Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
     
    		Static oColumnLefts As New ArrayList
    		Static oColumnWidths As New ArrayList
    		Static oColumnTypes As New ArrayList
    		Static nHeight As Int16
     
    		Dim nWidth, i, nRowsPerPage As Int16
    		Dim nTop As Int16 = e.MarginBounds.Top
    		Dim nLeft As Int16 = e.MarginBounds.Left
     
    		If nPageNo = 1 Then
     
    			For Each oColumn As DataGridViewColumn In DataGridView1.Columns
     
    				nWidth = CType(Math.Floor(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)
     
    				nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 11
     
    				oColumnLefts.Add(nLeft)
    				oColumnWidths.Add(nWidth)
    				oColumnTypes.Add(oColumn.GetType)
    				nLeft += nWidth
     
    			Next
     
    		End If
     
    		Do While nRowPos < DataGridView1.Rows.Count - 1
     
    			Dim oRow As DataGridViewRow = DataGridView1.Rows(nRowPos)
     
    			If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
     
    				DrawFooter(e, nRowsPerPage)
     
    				NewPage = True
    				nPageNo += 1
    				e.HasMorePages = True
    				Exit Sub
     
    			Else
     
    				If NewPage Then
     
    					' Draw Header
    					e.Graphics.DrawString(Header, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
     
    					' Draw Columns
    					nTop = e.MarginBounds.Top
    					i = 0
    					For Each oColumn As DataGridViewColumn In DataGridView1.Columns
     
    						e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
    						e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
    						e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
    						i += 1
     
    					Next
    					NewPage = False
     
    				End If
     
    				nTop += nHeight
    				i = 0
    				For Each oCell As DataGridViewCell In oRow.Cells
     
    					If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
     
    						e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
     
    					ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
     
    						oButton.Text = oCell.Value.ToString
    						oButton.Size = New Size(oColumnWidths(i), nHeight)
    						Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
    						oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
    						e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
     
    					ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
     
    						oCheckbox.Size = New Size(14, 14)
    						oCheckbox.Checked = CType(oCell.Value, Boolean)
    						Dim oBitmap As New Bitmap(oColumnWidths(i), nHeight)
    						Dim oTempGraphics As Graphics = Graphics.FromImage(oBitmap)
    						oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
    						oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width - oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) / 2, Int32), oCheckbox.Width, oCheckbox.Height))
    						e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
     
    					ElseIf oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
     
    						oComboBox.Size = New Size(oColumnWidths(i), nHeight)
    						Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
    						oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
    						e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
    						e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight), oStringFormatComboBox)
     
    					ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn) Then
     
    						Dim oCellSize As Rectangle = New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)
    						Dim oImageSize As Size = CType(oCell.Value, Image).Size
    						e.Graphics.DrawImage(oCell.Value, New Rectangle(oColumnLefts(i) + CType(((oCellSize.Width - oImageSize.Width) / 2), Int32), nTop + CType(((oCellSize.Height - oImageSize.Height) / 2), Int32), CType(oCell.Value, Image).Width, CType(oCell.Value, Image).Height))
     
    					End If
     
    					e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
     
    					i += 1
     
    				Next
     
    			End If
     
    			nRowPos += 1
    			nRowsPerPage += 1
     
    		Loop
     
    		DrawFooter(e, nRowsPerPage)
     
    		e.HasMorePages = False
     
    	End Sub
     
    	Private Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
     
    		Dim sPageNo As String = nPageNo.ToString + " of " + Math.Ceiling(DataGridView1.Rows.Count / RowsPerPage).ToString
     
    		' Right Align - User Name
    		e.Graphics.DrawString(sUserName, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 7)
     
    		' Left Align - Date/Time
    		e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height + 7)
     
    		' Center  - Page No. Info
    		e.Graphics.DrawString(sPageNo, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31)
     
    	End Sub
     
     
    End Class

    Fichiers joints:la sortie imprimee (fichier pdf) et le .mdb personnel.rar de test utilise.....
    Est qu'on peut avoir un apercu de l'environnement (base de donne,vs 2008 ou 2010,driver pdf.....)................
    bon code........

  17. #17
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    bonjour mabrouki,
    alors, en effet, cela fonctionne pour l'impression en se basant sur le mdb que tu m'as envoyé mais si je rajoute une ligne manuellement, cela me fait le même plantage...
    on ne peut pas imprimer un dgv si il est rempli manuellement par l'utilisateur?

    parce que vu que je dois faire un soft de gestion des ventes, j'avais pensé que l'utilisateur remplirait le dgv manuellement, une colonne par jour...
    car l'avantage, c'est qu'en rentrant les ventes, les stocks se mettent direct à jour en visuel et le boss peut voir si il faut faire des dépannages...

    mon environnement:
    vs 2010
    base de données sdf (base de données locale crée dans vs)
    pour les drivers mais pas besoin vu que le code marche si pas d'ajouts manuels: acrobat pro 9


    merci énormément de ton aide en tout cas!

  18. #18
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour wizard

    on ne peut pas imprimer un dgv si il est rempli manuellement par l'utilisateur?
    Si si,meme en cas d'ajout de lignes.
    Si tu as compile tel quel mon exemple, l'ajout de lignes est à false car j'ai pris le dgv d'un autre projet sans faire attention à cet aspect(regardre dans les prop du dgv ).
    Par consequent met le à True dans les prop du dgv ou bien par code
    Mon exemple auarait besoin qu'on rajoute ces lignes :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
     
    'Fill du DataGridView
    	Private Sub SetupDataGridView()
    		'Le datasource PersonnelDS et la table Personnel
     
    		'IL TE MANQUE PEUT ETRE CECI
    		DataGridView1.AllowUserToAddRows = True
    		DataGridView1.AllowUserToDeleteRows = True
    		DataGridView1.AllowUserToResizeRows = True
    		DataGridView1.AllowUserToOrderColumns = True
    		DataGridView1.AllowUserToResizeColumns = True
    		'Clear the DataGridViewLanguages control
    		DataGridView1.DataSource = Nothing
    		Me.Personnel1TableAdapter1.Fill(Me.PersonnelDS1.Personnel1)
    		DataGridView1.DataSource = Me.PersonnelDS1
    		DataGridView1.DataMember = Me.PersonnelDS1.Personnel1.TableName
     
     
    		Me.btnPrintDGV.Enabled = True
    	End Sub
    bon code...................

  19. #19
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    bonjour!
    alors, c'est un peu bizarre:
    j'ai modifié le code comme tu me l'as envoyé, cela plante au même stade mais sans messages d'erreur;
    les allowuserto étaient tous sur true sauf le order columns;
    je me suis dit que j'allais tester en mettant le add rows sur false et devine quoi, cela fonctionne!
    une explication?

    bon j'ai 2-3 petits problèmes que je vais essayer de résoudre...
    merci énormément en tout cas

  20. #20
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2011
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Corse (Corse)

    Informations forums :
    Inscription : Septembre 2011
    Messages : 92
    Points : 63
    Points
    63
    Par défaut
    aurais-tu une idée de comment je pourrais lui demander de n'imprimer que les colonnes visibles par hasard?

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. remplir datagrid view
    Par Youssef dans le forum Accès aux données
    Réponses: 4
    Dernier message: 02/04/2007, 16h22
  2. [VB6] Imprimer un datagrid lié avec ADO
    Par lepiou dans le forum VB 6 et antérieur
    Réponses: 5
    Dernier message: 12/06/2006, 11h38
  3. [VB.Net] Comment imprimer un DataGrid ?
    Par JauB dans le forum ASP.NET
    Réponses: 3
    Dernier message: 09/05/2006, 14h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo