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

Windows Forms Discussion :

Comment imprimer les données de dataGrid?


Sujet :

Windows Forms

  1. #1
    Débutant  
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    1 571
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 1 571
    Points : 353
    Points
    353
    Par défaut Comment imprimer les données de dataGrid?
    Bonjour tout le monde,

    j'ai une première fenêtre ("SuiviIntervention") que je souhaite imprimer en transvasant ses données dans une deuxième forms ("Form2").

    Voici le code que j'utilise dans la première form :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    private void Imprimer_Click(object sender, System.EventArgs e)
    		{
    Form2 F2 = new Form2();
    			F2.UpdateValues(dataGrid1);
    			F2.ShowDialog();
    		}
    Et voici le code que j'utilise dans la deuxième form :
    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
    using System.Drawing.Printing;
     
    private const int WM_PRINT = 0x0317;
    		private const int PRF_CLIENT = 0x00000004;
    		private const int PRF_CHILDREN = 0x00000010;
     
    		public void UpdateValues(string DG)
    		{
    			dataGrid1.Show();
    		}
     
    		public Bitmap PrintWindowEx()
    		{
    			Bitmap bmp = null;
    			Graphics gr = null;
    			IntPtr hdc = IntPtr.Zero;
    			try
    			{
    				bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, this.CreateGraphics());
    				gr = Graphics.FromImage(bmp);
    				hdc = gr.GetHdc();
    				IntPtr wParam = hdc;
    				IntPtr lParam = new IntPtr(PRF_CLIENT | PRF_CHILDREN);
    				Message msg = Message.Create(this.Handle, WM_PRINT, wParam, lParam);
    				this.WndProc(ref msg);
    			}
    			catch { }
    			finally
    			{
    				if (gr != null)
    				{
    					if (hdc != IntPtr.Zero)
    						gr.ReleaseHdc(hdc);
    					gr.Dispose();
    				}
    			}
    			return bmp;
    		}
     
    		private void Imprimer_Click(object sender, System.EventArgs e)
    		{
    			Imprimer.Visible = false;
     
    			PrintDocument pd = new PrintDocument();
     
    			// évènement déclenché juste avant l'impression pour obtenir un dessin
    			pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); 
     
    			// lancement de l'impression
    			pd.Print(); 
    		}
     
    		private void pd_PrintPage(object sender, PrintPageEventArgs e) 
    		{ 
    			// Là c'est comme si tu fais un dessin normal 
    			Graphics dc = e.Graphics; 
     
    			e.Graphics.DrawImage(PrintWindowEx(), new PointF(50, 10));
     
    			// Test s'il n'y a plus aucune page à imprimer 
    			if ( dc == null ) 
    				e.HasMorePages = true;
    			else 
    				e.HasMorePages = false;  
    		}
    Comment je peux faire pour mette les données du dataGrid1 de la première form dans un autre dataGrid dans la form2??

    Cordialement.

  2. #2
    Débutant
    Inscrit en
    Mars 2006
    Messages
    492
    Détails du profil
    Informations forums :
    Inscription : Mars 2006
    Messages : 492
    Points : 94
    Points
    94
    Par défaut
    essay ça :

    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
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
     
     
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Collections;
    using System.Data;
    using System.Text;
     
     
    namespace TALCOM
    {
        class PrintTAL
        {
            private static StringFormat StrFormat;  // Holds content of a TextBox Cell to write by DrawString
            private static StringFormat StrFormatComboBox; // Holds content of a Boolean Cell to write by DrawImage
            private static Button CellButton;       // Holds the Contents of Button Cell
            private static CheckBox CellCheckBox;   // Holds the Contents of CheckBox Cell 
            private static ComboBox CellComboBox;   // Holds the Contents of ComboBox Cell
            private static int TotalWidth;          // Summation of Columns widths
            private static int RowPos;              // Position of currently printing row 
            private static bool NewPage;            // Indicates if a new page reached
            private static int PageNo;              // Number of pages to print
            private static ArrayList ColumnLefts = new ArrayList();  // Left Coordinate of Columns
            private static ArrayList ColumnWidths = new ArrayList(); // Width of Columns
            private static ArrayList ColumnTypes = new ArrayList();  // DataType of Columns
            private static int CellHeight;          // Height of DataGrid Cell
            private static int RowsPerPage;         // Number of Rows per Page
            private static System.Drawing.Printing.PrintDocument printDoc =
                           new System.Drawing.Printing.PrintDocument();  // PrintDocumnet Object used for printing
     
            private static string PrintTitle = "";  // Header of pages
            private static DataGridView dgv;        // Holds DataGridView Object to print its contents
            private static List<string> SelectedColumns = new List<string>();   // The Columns Selected by user to print.
            private static List<string> AvailableColumns = new List<string>();  // All Columns avaiable in DataGrid 
            private static bool PrintAllRows = true;   // True = print all rows,  False = print selected rows    
            private static bool FitToPageWidth = true; // True = Fits selected columns to page width ,  False = Print columns as showed    
            private static int HeaderHeight = 0;
     
            public static void Print_DataGridView(DataGridView dgv1)
            {
                PrintPreviewDialog ppvw;
                try
                {
                    // Getting DataGridView object to print
                    dgv = dgv1;
     
                    // Getting all Coulmns Names in the DataGridView
                    AvailableColumns.Clear();
                    foreach (DataGridViewColumn c in dgv.Columns)
                    {
                        if (!c.Visible) continue;
                        AvailableColumns.Add(c.HeaderText);
                    }
                    // Showing the PrintOption Form
                    PrintOptions dlg = new PrintOptions(AvailableColumns);
                    if (dlg.ShowDialog() != DialogResult.OK) return;
     
                    PrintTitle = dlg.PrintTitle;
                    PrintAllRows = dlg.PrintAllRows;
                    FitToPageWidth = dlg.FitToPageWidth;
                    SelectedColumns = dlg.GetSelectedColumns();
     
                    RowsPerPage = 0;
     
                    ppvw = new PrintPreviewDialog();
                    ppvw.Document = printDoc;
     
                    // Showing the Print Preview Page
                    printDoc.BeginPrint += new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
                    printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
                    if (ppvw.ShowDialog() != DialogResult.OK)
                    {
                        printDoc.BeginPrint -= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
                        printDoc.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
                        return;
                    }
                    // Printing the Documnet
                    printDoc.Print();
                    printDoc.BeginPrint -= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
                    printDoc.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
     
                }
            }
            private static void PrintDoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
            {
                try
                {
                    // Formatting the Content of Text Cell to print
                    StrFormat = new StringFormat();
                    StrFormat.Alignment = StringAlignment.Near;
                    StrFormat.LineAlignment = StringAlignment.Center;
                    StrFormat.Trimming = StringTrimming.EllipsisCharacter;
     
                    // Formatting the Content of Combo Cells to print
                    StrFormatComboBox = new StringFormat();
                    StrFormatComboBox.LineAlignment = StringAlignment.Center;
                    StrFormatComboBox.FormatFlags = StringFormatFlags.NoWrap;
                    StrFormatComboBox.Trimming = StringTrimming.EllipsisCharacter;
     
                    ColumnLefts.Clear();
                    ColumnWidths.Clear();
                    ColumnTypes.Clear();
                    CellHeight = 0;
                    RowsPerPage = 0;
     
                    // For various column types
                    CellButton = new Button();
                    CellCheckBox = new CheckBox();
                    CellComboBox = new ComboBox();
     
                    // Calculating Total Widths
                    TotalWidth = 0;
                    foreach (DataGridViewColumn GridCol in dgv.Columns)
                    {
                        if (!GridCol.Visible) continue;
                        if (!PrintTAL.SelectedColumns.Contains(GridCol.HeaderText)) continue;
                        TotalWidth += GridCol.Width;
                    }
                    PageNo = 1;
                    NewPage = true;
                    RowPos = 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            private static void PrintDoc_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
            {
                int tmpWidth, i;
                int tmpTop = e.MarginBounds.Top;
                int tmpLeft = e.MarginBounds.Left;
     
                try
                {
                    // Before starting first page, it saves Width & Height of Headers and CoulmnType
                    if (PageNo == 1)
                    {
                        foreach (DataGridViewColumn GridCol in dgv.Columns)
                        {
                            if (!GridCol.Visible) continue;
                            // Skip if the current column not selected
                            if (!PrintTAL.SelectedColumns.Contains(GridCol.HeaderText)) continue;
     
                            // Detemining whether the columns are fitted to page or not.
                            if (FitToPageWidth)
                                tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                           (double)TotalWidth * (double)TotalWidth *
                                           ((double)e.MarginBounds.Width / (double)TotalWidth))));
                            else
                                tmpWidth = GridCol.Width;
     
                            HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                        GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;
     
                            // Save width & height of headres and ColumnType
                            ColumnLefts.Add(tmpLeft);
                            ColumnWidths.Add(tmpWidth);
                            ColumnTypes.Add(GridCol.GetType());
                            tmpLeft += tmpWidth;
                        }
                    }
     
                    // Printing Current Page, Row by Row
                    while (RowPos <= dgv.Rows.Count - 1)
                    {
                        DataGridViewRow GridRow = dgv.Rows[RowPos];
                        if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
                        {
                            RowPos++;
                            continue;
                        }
     
                        CellHeight = GridRow.Height;
     
                        if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                        {
                            DrawFooter(e, RowsPerPage);
                            NewPage = true;
                            PageNo++;
                            e.HasMorePages = true;
                            return;
                        }
                        else
                        {
                            if (NewPage)
                            {
                                Font drawF = new Font("Arial", 14, FontStyle.Bold);
                                e.Graphics.DrawString(PrintTitle, drawF,
                                          Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
                                  e.Graphics.MeasureString(PrintTitle, new Font(dgv.Font,
                                          FontStyle.Bold), e.MarginBounds.Width).Height +70);
     
                                try
                                {
                                    string strFile1 = "C:\\tal.jpg";
                                 Image img1 = Image.FromFile(strFile1);
                                 e.Graphics.DrawImage(img1,160, 15); 
                                }
                                catch
                                {MessageBox.Show("il y'a une erreur!!!!"); } 
     
                                try
                                {string strFile2 = "C:\\iata.jpg";
                                 Image img2 = Image.FromFile(strFile2);
                                 e.Graphics.DrawImage(img2, 750, 1050);
                                }
                                catch
                                {MessageBox.Show("il y'a une erreur!!!!");}
     
     
     
     
     
     
         String s = " Le " + DateTime.Now.ToShortDateString(); 
          e.Graphics.DrawString(s, new Font(dgv.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.X+610, e.MarginBounds.Y+30);
        Pen blackPen1 = new Pen(Color.Black, 3);
        Pen blackPen2 = new Pen(Color.Black, 1);
     
        // Create points that define line.
        Point point1 = new Point(30, 150);
        Point point2 = new Point(800, 150);
        Point point9 = new Point(30, 146);
        Point point10 = new Point(800, 146);
     
        Point point3 = new Point(620, 170);
        Point point4 = new Point(620, 150);
        Point point5 = new Point(799, 170);
        Point point6 = new Point(799, 150);
     
        Point point7 = new Point(30, 1050);
        Point point8 = new Point(800, 1050);
        Point point11 = new Point(30, 1055);
        Point point12 = new Point(750, 1055);
     
        e.Graphics.DrawLine(blackPen1, point1, point2);
        //e.Graphics.DrawLine(blackPen1, point3, point4);
        //e.Graphics.DrawLine(blackPen1, point5, point6);
        e.Graphics.DrawLine(blackPen1, point7, point8);
        e.Graphics.DrawLine(blackPen2, point9, point10);
        e.Graphics.DrawLine(blackPen2, point11, point12);
     
                                // Draw Columns
                                tmpTop = e.MarginBounds.X+100;
                                i = 0;
                                foreach (DataGridViewColumn GridCol in dgv.Columns)
                                {   if (!GridCol.Visible) continue;
                                    if (!PrintTAL.SelectedColumns.Contains(GridCol.HeaderText))
                                      continue;
                                    e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                    new Rectangle((int)ColumnLefts[i], tmpTop,
                                    (int)ColumnWidths[i], HeaderHeight));
                                    e.Graphics.DrawRectangle(Pens.Black,
                                        new Rectangle((int)ColumnLefts[i], tmpTop,
                                        (int)ColumnWidths[i], HeaderHeight));
                                    e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                                        new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                        new RectangleF((int)ColumnLefts[i], tmpTop,
                                        (int)ColumnWidths[i], HeaderHeight), StrFormat);
                                    i++;
                                }
                                NewPage = false;
                                tmpTop += HeaderHeight;
                            }
     
                            // Draw Columns Contents
                            i = 0;
                            foreach (DataGridViewCell Cel in GridRow.Cells)
                            {
                                if (!Cel.OwningColumn.Visible) continue;
                                if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
                                    continue;
     
                                // For the TextBox Column
                                if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
                                    ((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
                                {
                                    e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                            new SolidBrush(Cel.InheritedStyle.ForeColor),
                                            new RectangleF((int)ColumnLefts[i], (float)tmpTop,
                                            (int)ColumnWidths[i], (float)CellHeight), StrFormat);
                                }
                                // For the Button Column
                                else if (((Type)ColumnTypes[i]).Name == "DataGridViewButtonColumn")
                                {
                                    CellButton.Text = Cel.Value.ToString();
                                    CellButton.Size = new Size((int)ColumnWidths[i], CellHeight);
                                    Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
                                    CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
                                            bmp.Width, bmp.Height));
                                    e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                }
                                // For the CheckBox Column
                                else if (((Type)ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
                                {
                                    CellCheckBox.Size = new Size(14, 14);
                                    CellCheckBox.Checked = (bool)Cel.Value;
                                    Bitmap bmp = new Bitmap((int)ColumnWidths[i], CellHeight);
                                    Graphics tmpGraphics = Graphics.FromImage(bmp);
                                    tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
                                            bmp.Width, bmp.Height));
                                    CellCheckBox.DrawToBitmap(bmp,
                                            new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
                                            (int)((bmp.Height - CellCheckBox.Height) / 2),
                                            CellCheckBox.Width, CellCheckBox.Height));
                                    e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                }
                                // For the ComboBox Column
                                else if (((Type)ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
                                {
                                    CellComboBox.Size = new Size((int)ColumnWidths[i], CellHeight);
                                    Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
                                    CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0,
                                            bmp.Width, bmp.Height));
                                    e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
                                    e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                            new SolidBrush(Cel.InheritedStyle.ForeColor),
                                            new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i]
                                            - 16, CellHeight), StrFormatComboBox);
                                }
                                // For the Image Column
                                else if (((Type)ColumnTypes[i]).Name == "DataGridViewImageColumn")
                                {
                                    Rectangle CelSize = new Rectangle((int)ColumnLefts[i],
                                            tmpTop, (int)ColumnWidths[i], CellHeight);
                                    Size ImgSize = ((Image)(Cel.FormattedValue)).Size;
                                    e.Graphics.DrawImage((Image)Cel.FormattedValue,
                                            new Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2),
                                            tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2),
                                            ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));
     
                                }
     
                                // Drawing Cells Borders 
                                e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i],
                                        tmpTop, (int)ColumnWidths[i], CellHeight));
     
                                i++;
     
                            }
                            tmpTop += CellHeight;
                        }
     
                        RowPos++;
                        // For the first page it calculates Rows per Page
                        if (PageNo == 1) RowsPerPage++;
                    }
     
                    if (RowsPerPage == 0) return;
     
                    // Write Footer (Page Number)
                    //DrawFooter(e, RowsPerPage);
     
                    e.HasMorePages = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
     
     
            private static void DrawFooter(System.Drawing.Printing.PrintPageEventArgs e,
                                int RowsPerPage)
            {
                double cnt = 0;
     
                // Detemining rows number to print
                if (PrintAllRows)
                {
                    if (dgv.Rows[dgv.Rows.Count - 1].IsNewRow)
                        cnt = dgv.Rows.Count - 2; // When the DataGridView doesn't allow adding rows
                    else
                        cnt = dgv.Rows.Count - 1; // When the DataGridView allows adding rows
                }
                else
                    cnt = dgv.SelectedRows.Count;
     
                // Writing the Page Number on the Bottom of Page
                string PageNum = PageNo.ToString() + " of " +
                    Math.Ceiling((double)(cnt / RowsPerPage)).ToString();
     
                e.Graphics.DrawString(PageNum, dgv.Font, Brushes.Black,
                    e.MarginBounds.Left + (e.MarginBounds.Width -
                    e.Graphics.MeasureString(PageNum, dgv.Font,
                    e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top +
                    e.MarginBounds.Height + 31);
            }
     
        }
    }

  3. #3
    Débutant  
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    1 571
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 1 571
    Points : 353
    Points
    353
    Par défaut réponse à abbd
    Excuse moi mais ton code c'est pas pour les dataGridView??
    Moi j'utilise un dataGrid et non un dataGridView.
    Je suis sur Visual Studio 2003.
    Je veux pas juste imprimer le dataGrid, car j'ai plusieurs choses a imprimer dans la deuxième forms.

    Je voudrais insérer le contenu de mon dataGrid de la première form dans un dataGrid de la deuxième form pour ensuite imprimer ma deuxième form.
    J'ai essayé cela :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    private void Imprimer_Click(object sender, System.EventArgs e)
    		{
    Form2 F2 = new Form2();
    			F2.UpdateValues(NumInter.Text, NomClt.Text, LieuInter.Text, EtatPanne.Text, RaisonInter.Text, EtatInter.Text, DatePrisEnCharge.Text, DateDispo.Text, DateRecup.Text, ComPanne.Text, ComReso.Text, DureeMoy.Text, NbreHeurePasse.Text, NbreHeureFacture.Text, NomIntervenant.Text);
    			F2.ShowDialog(); 
    			F2.dataGrid1.DataSource = this.dataGrid1.DataSource;
    		}
    mais lorsque j'exécute mon programme, le contenu de la form1 ne remplit pas la form2.

    Voici comment je remplis le dataGrid de la form1 :
    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
    // Code permettant de remplir le DataGrid des 5 dernières interventions.
    			string Five_Last_Intervention = ("SELECT  TOP 5 INTERVENTION.IN_NUMINTER as NumInter, CONTACT.CT_NOMCTACT as NomCtact, MACHINE.M_NOMMAT as NomMat FROM INTERVENTION INNER JOIN MACHINE ON INTERVENTION.IN_IDMAT = MACHINE.M_IDMAT AND INTERVENTION.IN_NUMCLT = dbo.MACHINE.M_NUMCLT INNER JOIN CONTACT ON MACHINE.M_NUMCTACT = CONTACT.CT_NUMCTACT WHERE INTERVENTION.IN_NUMCLT = '" + NumClt.Text + "' ORDER BY INTERVENTION.IN_NUMINTER DESC");
    			connection = new SqlConnection("Data Source=xpsp2-49f3e18f9;Initial Catalog=DistribInfo;Integrated Security=SSPI");
    			connection.Open();
    			try
    			{
    				SqlDataAdapter dataAdapter = new SqlDataAdapter(Five_Last_Intervention, connection);
    				DataSet ds = new DataSet();
    				dataAdapter.Fill(ds,"Five_Last_Intervention");
    				dataGrid1.DataSource = ds;
    				dataGrid1.DataMember = "Five_Last_Intervention";
    			}
    			catch(Exception ex)
    			{
    				MessageBox.Show(ex.ToString());
    			}
    			finally
    			{
    				connection.Close();
    			}
    Cordialement.

  4. #4
    Débutant  
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    1 571
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 1 571
    Points : 353
    Points
    353
    Par défaut
    J'ai réussis en faisant la chose suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    F2.dataGrid1.DataSource = this.dataGrid1.DataSource;
    			F2.dataGrid1.DataMember =  this.dataGrid1.DataMember;
    Cordialement.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 31/07/2009, 08h12
  2. comment afficher les données dans un datagrid
    Par tro2blabla dans le forum VB.NET
    Réponses: 7
    Dernier message: 13/08/2008, 08h58
  3. Réponses: 3
    Dernier message: 14/03/2005, 19h02
  4. Comment imprimer les FAQ
    Par tran dans le forum Mode d'emploi & aide aux nouveaux
    Réponses: 1
    Dernier message: 22/02/2005, 19h15

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