2017-03-20 126 views
1

在下面的代碼在打印預覽它顯示從DataGridView的所有數據,但不打印的一切只是印刷不從DataGridView打印數據而在打印預覽顯示

我已附加的圖像的頁面的結構的數據這兩個打印預覽和打印輸出

Print preview

private void btnPrintPreview_Click(object sender, EventArgs e) 
{ 
    printPreviewDialog1.ShowDialog(); 
    i = 0; 
} 

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
{ 
    e.Graphics.DrawString("Purchase Order ", new Font("Arial", 28, FontStyle.Bold), Brushes.Black, new Point(280, 10)); 
    e.Graphics.DrawString("POPULAR ", new Font("Arial", 20, FontStyle.Bold), Brushes.Black, new Point(10, 50)); 
    e.Graphics.DrawString("Center", new Font("Arial", 14, FontStyle.Regular), Brushes.Black, new Point(20, 80)); 
    e.Graphics.DrawString("Behind ,", new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 100)); 
    e.Graphics.DrawString(" Colony,", new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 120)); 
    e.Graphics.DrawString("Sh - 501.", new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 140)); 
    e.Graphics.DrawString("TIN : " + DateTime.Now.ToShortDateString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 160)); 
    e.Graphics.DrawString("GST : " + DateTime.Now.ToShortDateString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 178)); 

    e.Graphics.DrawString("P O No. : " + lblPONumber.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, 197)); 
    e.Graphics.DrawString("Date : " + date.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(506, 197)); 

    e.Graphics.DrawString("Customer Name : " + txtSupplierName.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 70)); 
    e.Graphics.DrawString("Company Name : " + txtSupplierCompanyName.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 90)); 
    e.Graphics.DrawString("Mobile No.   : " + txtMobile.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 110)); 
    e.Graphics.DrawString("Address    : ", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 130)); 
    e.Graphics.DrawString("        ", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 150)); 
    e.Graphics.DrawString("City      : ", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 170)); 


    e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, 210)); 

    e.Graphics.DrawString("Sl No.", new Font("Arial", 10, FontStyle.Regular), Brushes.Black, new Point(05, 227)); 
    e.Graphics.DrawString("Item Name", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(55, 225)); 
    e.Graphics.DrawString("Quantity", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(380, 225)); 
    e.Graphics.DrawString("Unit Price", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(510, 225)); 
    e.Graphics.DrawString("Total", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(660, 225)); 

    e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, 235)); 

    int ypos = 250; 

    while (i < dataGridView1.Rows.Count) 
    { 

     if (ypos > e.MarginBounds.Height) 
     { 
      ypos = 250; 
      e.HasMorePages = true; 
      return; 
     } 
      numberofitemsprinted++; 
      SlNumber++; 

       e.Graphics.DrawString(SlNumber.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, ypos)); 

       e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(60, ypos)); 

       e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(397, ypos)); 

       e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(530, ypos)); 

       e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(670, ypos)); 

       i++; 
       ypos = ypos + 30; 

    } 




     e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, ypos)); 

     e.Graphics.DrawString("Total Amount  :  " + txtTotalSum.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(510, ypos + 30)); 


     e.Graphics.DrawString("Amount in Words  :  " + txtTotalSumWords.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(200, ypos + 120)); 



     numberofitemsperpage = 0; 
     numberofitemsprinted = 0; 
     SlNumber = 0; 


    } 

private void btnPrint_Click(object sender, EventArgs e) 
{ 
    printDocument1.Print(); 
} 

回答

0

您需要在打印前變量i復位。

它看起來好像PrintPage被稱爲一次的預覽,然後在不i被重置,再次的實際印刷..

這應有助於:

private void btnPrint_Click(object sender, EventArgs e) 
{ 
    i = 0; 
    printDocument1.Print(); 
} 

我也強烈建議將名稱i更改爲更明確的東西,如currentPrintRow ..

+0

非常感謝@TaW – Sahara

+0

如果您對答案感到滿意,請考慮考慮[接受](http://stackoverflow.com/help/accepted-answer)它..! - 我發現你從來沒有這樣做過:在答案的選票下面,點擊左上角的(不可見)複選標記,然後單擊它!它變成綠色,並獲得我們兩個小聲譽.. – TaW

+0

是的兄弟@Taw與您的建議我得到了答案,我感謝你,但我不知道如何接受它作爲答案,所以添加了評論。 – Sahara