Datagridview imprimer

Résolu
mohamed222222 Messages postés 161 Date d'inscription mardi 12 juin 2007 Statut Membre Dernière intervention 22 avril 2009 - 30 oct. 2008 à 14:53
mohamed222222 Messages postés 161 Date d'inscription mardi 12 juin 2007 Statut Membre Dernière intervention 22 avril 2009 - 31 oct. 2008 à 15:22
Bonjour,

je souhaiterai imprimer le contenu de mon datagridview. pour cela j'utilise le code suivant :

Private
Sub PrintDocument1_PrintPage(
ByVal sender
As System.Object,
ByVal e
As System.Drawing.Printing.PrintPageEventArgs)
Handles PrintDocument1.PrintPage

Dim myPaintArgs
As
New PaintEventArgs(e.Graphics,
New Rectangle(
New Point(0, 0),
Me.Size))

Me.InvokePaint(DataGridViewListAno, myPaintArgs)

End
Sub

-----------------------------
Private
Sub bImprimer_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles bImprimer.ClickPrintDocument1.Print()

End
Sub

J'ai bien une impression mais juste la structure du datagridview mais pas le contenu et je souhaiterai également ajouter des informations sur la page (date, titre,...)
Comment Faire ?

Quelqu'un peut m'aider SVP !
Merci d'avance.

6 réponses

gillardg Messages postés 3275 Date d'inscription jeudi 3 avril 2008 Statut Membre Dernière intervention 14 septembre 2014 2
30 oct. 2008 à 16:27
3
lillith212 Messages postés 1229 Date d'inscription vendredi 16 novembre 2007 Statut Membre Dernière intervention 16 juin 2009
30 oct. 2008 à 14:58
Bonjour,


As-tu fais un petit tour ici :



http://www.vbfrance.com/codes/IMPRIMER-DATAGRID-SOLUTION_6522.aspx



Bon courage



S.L.B.
<hr />-- Le règlement tu liras -- Des recherches tu feras -- Le style SMS tu banniras --
-- De la validation pertinente tu feras -- Du respect tu auras -- <
0
mohamed222222 Messages postés 161 Date d'inscription mardi 12 juin 2007 Statut Membre Dernière intervention 22 avril 2009
30 oct. 2008 à 15:10
J'ai déja vu ce tuto mais il ma rien apporter.  je  ne vois pas la solution.

Tu peux m'aider autrement STP ?

Merci d'avance
0
mohamed222222 Messages postés 161 Date d'inscription mardi 12 juin 2007 Statut Membre Dernière intervention 22 avril 2009
30 oct. 2008 à 16:15
de l'aide SVP je suis entrain de couler ...........
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
mohamed222222 Messages postés 161 Date d'inscription mardi 12 juin 2007 Statut Membre Dernière intervention 22 avril 2009
30 oct. 2008 à 17:02
Merci mais j'ai déja vu ce tuto. J'ai vraiment du mal...
0
mohamed222222 Messages postés 161 Date d'inscription mardi 12 juin 2007 Statut Membre Dernière intervention 22 avril 2009
31 oct. 2008 à 15:22
En fait grace à ton lien j'ai réussi à imprimmer mon datagrid . j'ai utilisé ce code :

Imports

Microsoft.VisualBasic
Imports

System
Imports

System.ComponentModel
Imports

System.Collections.Generic
Imports

System.Diagnostics
Imports

System.Text
Imports

System.Drawing.Printing
Imports

System.Windows.Forms
Imports

System.Drawing
Imports

System.Collections
Namespace

PrintControl

Partial
Public
Class ControlPrint

Inherits PrintDocument

Private m_ctrl
As Control

''''

''''code original en c#

''''http://www.codeproject.com/KB/printing/ControlPrint.aspx

''''

''''

Private stretch
As
Boolean

''' <summary>

''' Set true to stretch the control to fill a single printed page

''' </summary>

Public
Property StretchControl()
As
Boolean

Set(
ByVal value
As
Boolean)stretch = value

End
Set

Get

Return stretch

End
Get

End
Property

Private width
As
Integer

Private height
As
Integer

''' <summary>

''' The width of the control to print. You can change this value if the automatically

''' calculated width does not fit all the elements of the control

''' </summary>

Public
Property PrintWidth()
As
Integer

Set(
ByVal value
As
Integer)width = value

End
Set

Get

Return width

End
Get

End
Property

''' <summary>

''' The height of the control to print. You can change this value if the automatically

''' calculated height does not fit all the elements of the control

''' </summary>

Public
Property PrintHeight()
As
Integer

Set(
ByVal value
As
Integer)height = value

End
Set

Get

Return height

End
Get

End
Property

Private inbetween
As
Integer

''' <summary>

''' The area to be reprinted between pages if there are more than one

''' pages to be printed, to prevent data loss

''' </summary>

Public
Property RepeatArea()
As
Integer

Get

Return inbetween

End
Get

Set(
ByVal value
As
Integer)inbetween = value

End
Set

End
Property

''' <summary>

''' Default constructor

''' </summary>

Public
SubNew()m_ctrl New Control()stretch

Falsewidth = m_ctrl.Width

height = m_ctrl.Height

inbetween = 10

End
Sub

''' <summary>

''' Intialize the component with the selected control

''' </summary>

''' The control to printed

Public
Sub
New(
ByVal print
As Control)SetControl(print)
stretch Falseinbetween 10

End
Sub

''' <summary>

''' Set the control to be printed

''' </summary>

''' The control you wish to print

Public
Sub SetControl(
ByVal print
As Control)m_ctrl = print

Dim NewSize As Size m_ctrl.GetPreferredSize(Size.Empty)width NewSize.Width

height = NewSize.Height

printedheight = 0

End
Sub

Private printedheight
As
Integer

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
NothingParents.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 scrollbarsm_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
Thenprintheight =

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)
Thene.HasMorePages =

True

Elsee.HasMorePages =

False

End
If 

Dim gp
As GraphicsUnit = GraphicsUnit.Point

Dim b
As
New Bitmap(width, height)

'Good, now we can drawm_ctrl.DrawToBitmap(b,

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

'Will we stretch the image?

If stretch
Thene.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
Thenprintheight = 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 RepeatAreaprintedheight += e.MarginBounds.Height - inbetween

Elseprintedheight = 0

End
If

'Restore the control's state

Dim i
As
Integer = 0

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

i += 1
Loopm_ctrl.Size New Size(OldSize.Width, OldSize.Height)m_ctrl.Dock OldDock

End
Sub

End
ClassEnd

Namespace

Cela marche j'ai bien mon datagridview par contre je souhaiterai rajouter un titre , une date, numéro de page et le mettre au format paysage.

Merci d'avance.....
0
Rejoignez-nous