2016-05-22 32 views
-2

私は父親のビジネスのためにアプリケーションから請求書を印刷しようとしていますが、次のコードを使用して請求書を印刷しています。文書に印刷する

私の試みは以下の通りです:私は、プレビューボタンをクリックしたときに、ここで

{ 

int y = 470; 
     while (i < dataGridView1.RowCount) 
     {  
      e.Graphics.DrawString(dataGridView1.Rows[i].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(250, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(600, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[i].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(700, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y)); 
      y = y + 20; 
      if (y >= pageHeight) 
      { 

       e.HasMorePages = true; 
       y = 0; 
       i++; 
       return; 
      } 
      else 
      { 
       e.HasMorePages = false; 
      } 
      i++; 
     } 
} 

私は紙の上にそれを印刷するとき、私は期待通りの出力が得られますが、グローバル変数

private int i = 0;

ですwhileループの内容のみが出力されます。私はグローバル変数の代わりにローカル変数を使ってみましたが、以下に示すように動作しました。

for (int j=0; j < dataGridView1.RowCount; j++) 
     { 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(250, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(600, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(700, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y)); 
      y = y + 20; 
     } 

ただし、含める項目がページの高さを超える場合、新しいページを追加する方法はわかりません。助けてください。

+0

を解くコードは、[dreamincode](http://www.dreamincode.net/forums/topic/44330-printing- in-c%23 /)は、複数のページをプリンタに印刷する際に優れた記事です。 [MSDN](https://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.print(v = vs.110).aspx)にも、まともな例があります。 。 – Travis

答えて

0

コードをステップごとに繰り返した後、コードtpの印刷プレビューがフォームの印刷中に再度実行されることがわかりました。だから私はちょうど最初から印刷を開始するように0にi値を初期化しなければならなかった。ここ

は基本的にリンク唯一の答えを避けるためにそれを

int j; 
     for (j=i; j < dataGridView1.RowCount && y<e.MarginBounds.Bottom; j++) 
     { 
      e.Graphics.DrawString(e.MarginBounds.Bottom.ToString(), DefaultFont, Brushes.Black, new Point(100, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(220, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(770, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(620, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(670, y)); 
      e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(720, y)); 
      y = y + 20; 
     } 
     if (j < dataGridView1.RowCount) 
     { 
      e.HasMorePages = true; 
      i=j; 
     } 
     else 
     { 
      e.HasMorePages = false; 
      i = 0; 
     } 
関連する問題