2016-08-26 2 views
0

J'ai un dgv où j'ajoute beaucoup de produits et codes, valeurs etc, pour les produits, alors j'ai besoin d'imprimer le contenu.La meilleure façon d'imprimer des informations à partir d'un dgv

Dans un premier temps, je suis en utilisant ce code:

private int _Line = 0; 
     void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
     { 
      Font myFont = new Font("Courier New", 08, FontStyle.Underline, GraphicsUnit.Point); 

      float lineHeight = myFont.GetHeight(e.Graphics) + 4; 

      float yLineTop = e.MarginBounds.Top; 



      int b = dataGridView1.Rows.Count;    
      for (int _Line = 0; _Line < b; _Line++) 
      { 
       if (yLineTop + lineHeight > e.MarginBounds.Bottom) 
       { 
        e.HasMorePages = true; 
        return; 
       } 

       //e.Graphics.DrawString("TEST: " + _Line, myFont, Brushes.Black, 
       // new PointF(e.MarginBounds.Left, yLineTop)); 

       Graphics graphics = e.Graphics; 
       //Font font = new Font("Courier New", 8); 
       //float fontHeight = font.GetHeight(); 
       int startX = 50; 
       int startY = 65; 
       int Offset = 40; 
       graphics.DrawString("Welcome to Bakery Shop - "+DateTime.Now+".", new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset); 
       Offset = Offset + 20; 
       string underLine = "------------------------------------------"; 
       graphics.DrawString(underLine, new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset); 
       Offset = Offset + 20; 
       graphics.DrawString("" + label1.Text + "", new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset); 
       Offset = Offset + 20; 
       graphics.DrawString("Item", new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset); 
       graphics.DrawString("Cod.", new Font("Courier New", 8), new SolidBrush(Color.Black), startX+80, startY + Offset); 
       graphics.DrawString("Nome", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 160, startY + Offset); 
       graphics.DrawString("Valor", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 240, startY + Offset); 
       graphics.DrawString("Qtd.", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 320, startY + Offset); 
       graphics.DrawString("Parcial", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 400, startY + Offset); 
       graphics.DrawString("Desconto", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 510, startY + Offset); 
       graphics.DrawString("Subtotal", new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 600, startY + Offset); 
       Offset = Offset + 20; 
       int a = dataGridView1.Rows.Count; 
       for (int i = 0; i < a; i++) 

       { 

        graphics.DrawString(Convert.ToString(dataGridView1.Rows[i].Index+1), new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset + 30); 
        graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[0].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 10, startY + Offset + 30); 
        graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[1].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 90, startY + Offset + 30); 
        graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[2].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 180, startY + Offset + 30); 
        graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[3].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 270, startY + Offset + 30); 
        graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[4].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 340, startY + Offset + 30); 
        graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[5].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 430, startY + Offset + 30); 
        graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[6].Value), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 570, startY + Offset + 30); 
        Offset = Offset + 20; 

        graphics.DrawString("\t" +), new Font("Courier New", 8), new SolidBrush(Color.Black), startX + 570, startY + Offset + 30); 
        Offset = Offset + 20; 

       } 
       Offset = Offset + 20; 
       Offset = Offset + 20; 
       Offset = Offset + 20; 
       graphics.DrawString("Total - " + textBox7.Text + ".", new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset); 
       Offset = Offset + 20; 
       graphics.DrawString("Troco - " + textBox3.Text + ".", new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset); 
       Offset = Offset + 20; 
       yLineTop += lineHeight; 
      } 

      e.HasMorePages = false; 
     } 

A côté un comportement étrange que je vais travailler plus tard, je remarque qu'une seule ligne doit avoir chaque élément bien aligné de telle sorte que, il N'imprime pas les informations ci-dessus. Puis j'ai pensé que, si je convertis la ligne complète et ajoute un espace entre chaque cellule, et j'imprime la matrice, elle n'afficherait jamais l'information au-dessus de l'info, même si par exemple le "code" est trop grand, donc ne serait pas montré ci-dessus/mélangé avec la colonne "nom".

Est-ce une meilleure façon? Comment puis-je commencer à faire cela? Parce que partout où je regarde, la solution est de passer une seule cellule sous forme de chaîne.

Dans mon cas, chaque rangée, aurait 6 colonnes, merci beaucoup d'avance!

Répondre

0

fixe comme ceci:

int a = dataGridView1.Rows.Count;     
       for (int i = 0; i < a; i++)    

       { 
        string[] dgvtoarray = { Convert.ToString(dataGridView1.Rows[i].Index + 1), Convert.ToString(dataGridView1.Rows[i].Cells[0].Value), Convert.ToString(dataGridView1.Rows[i].Cells[1].Value), Convert.ToString(dataGridView1.Rows[i].Cells[2].Value), Convert.ToString(dataGridView1.Rows[i].Cells[3].Value), Convert.ToString(dataGridView1.Rows[i].Cells[4].Value), Convert.ToString(dataGridView1.Rows[i].Cells[5].Value), Convert.ToString(dataGridView1.Rows[i].Cells[6].Value) }; 
        var result = string.Join(" | ", dgvtoarray); 

        graphics.DrawString(Convert.ToString(result), new Font("Courier New", 8), new SolidBrush(Color.Black), startX, startY + Offset + 30); 
        Offset = Offset + 20; 

       } 
0

La meilleure façon d'imprimer à partir datagridview transfère des données à feuille de calcul Excel.

 Microsoft.Office.Interop.Excel.Worksheet ws; 
     try 
     { 
      Microsoft.Office.Interop.Excel.Application Excell = new Microsoft.Office.Interop.Excel.Application(); 
      Microsoft.Office.Interop.Excel.Workbook wb = Excell.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet); 
      ws = (Microsoft.Office.Interop.Excel.Worksheet)Excell.ActiveSheet; 
      Excell.Visible = true; 
     } 
     catch (Exception) 
     { 
      return; 
     } 

     int i = 1; 

     foreach (DataGridViewColumn clm in dgw.Columns) 
     { 
      ws.Cells[2, i] = clm.Name; 
      Microsoft.Office.Interop.Excel.Range xcell = ws.Cells[2, i]; 
      Microsoft.Office.Interop.Excel.Borders brd1 = xcell.Borders; 
      xcell.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; 

      brd1.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; 
      brd1.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium; 
      i++; 
     } 

     Microsoft.Office.Interop.Excel.Range row = ws.Cells[2, i]; 
     row.EntireRow.Font.Bold = true; 

     Microsoft.Office.Interop.Excel.Range fcell = ws.Cells[1, 1]; 
     Microsoft.Office.Interop.Excel.Range lcell = ws.Cells[1, i - 1]; 
     Microsoft.Office.Interop.Excel.Range space = ws.get_Range(fcell, lcell); 
     space.Merge(true); 
     space.EntireRow.Font.Bold = true; 

     ws.Cells[1, 1] = //Your text here 
     Microsoft.Office.Interop.Excel.Borders brd = space.Borders; 
     aralik.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; 

     int m = 3; 

     foreach (DataGridViewRow rows in dgw.Rows) 
     { 
      for (int p = 1; p < i; p++) 
      { 
       if (rows.Cells[p-1].Value.ToString()=="") 
       { 
        string str = string.Empty; 

        foreach (Control item in cList) 
        { 
         if (item.Name=="lbl_"+(rows.Index.ToString())+"_"+((p-1).ToString())) 
         { 
          str = item.Text; 
          ws.Cells[m, p] = str; 
         } 
        } 
       } 
       else 
       { 
        ws.Cells[m, p] = rows.Cells[p - 1].Value; 
       } 
      } 

      m++; 
     } 

     ws.Columns.AutoFit(); 

Ceci est mon code d'une application d'examen scolaire. J'espère que cela t'aides.