2016-04-08 89 views
-1

我無法獲得此數據表的內容跨越多個頁面。它在一頁上工作正常,但如果我使用e.HasMorePages,它會生成無限的打印預覽。任何援助將得到高度讚賞C#多頁打印

   using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using MySql.Data.MySqlClient; 
using System.Configuration; 
using System.Drawing.Printing; 
namespace TRUE_COMFORT_TENANT_MANAGEMENT_SYSTEM 
{ 
    public partial class uoccupied_rooms : Form 
    { 
     public uoccupied_rooms() 
     { 
      InitializeComponent(); 
     } 
     DataTable tab = null; 
     int pheight; 
     static int currentrow = 0; 

     private void uoccupied_rooms_Load(object sender, EventArgs e) 
     { 
      connection c = new connection(); 
      string cstring = c.mycon(); 
      MySqlConnection conn = new MySqlConnection(cstring); 
      conn.Open(); 
      MySqlDataAdapter da = new MySqlDataAdapter(); 
      string sql = "SELECT roomID AS 'Room ', roomtype AS 'Room Type' FROM rooms WHERE NOT EXISTS(SELECT roomID FROM occupancy WHERE rooms.`roomID`=occupancy.`room`);"; 
      da.SelectCommand = new MySqlCommand(sql, conn); 
      tab = new DataTable(); 
      da.Fill(tab); 
      BindingSource bsource = new BindingSource(); 
      bsource.DataSource = tab; 
      dataGridView1.DataSource = bsource; 
      int sum = dataGridView1.Rows.Count; 
      lbltotal.Text = sum.ToString() + " Rooms"; 
     } 

     private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
     { 
      Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height); 
      dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height)); 
      e.Graphics.DrawImage(bm, 0, 0); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      //printDocument1.Print(); 
      print(); 

     } 

     private void print() 
     { 
      PrintDocument pdoc = new PrintDocument(); 
      PrinterSettings ps = new PrinterSettings(); 
      Font font = new Font("Courier New", 15); 
      PrintDialog pd = new PrintDialog(); 
      PaperSize psize = new PaperSize("Custom", 100, 200); 
      pheight = psize.Height; 
      pd.Document = pdoc; 
      pd.Document.DefaultPageSettings.PaperSize = psize; 
      //pdoc.DefaultPageSettings.PaperSize.Height =320; 
      pdoc.DefaultPageSettings.PaperSize.Height = 820; 

      pdoc.DefaultPageSettings.PaperSize.Width = 520; 

      pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage); 
      DialogResult result = pd.ShowDialog(); 
      if (result == DialogResult.OK) 
      { 
       PrintPreviewDialog pp = new PrintPreviewDialog(); 
       pp.Document = pdoc; 
       result = pp.ShowDialog(); 
       if (result == DialogResult.OK) 
       { 
        pdoc.Print(); 
       } 
      } 

     } 
     void pdoc_PrintPage(object sender, PrintPageEventArgs e) 
     { 
      Graphics graphics = e.Graphics; 
      Font font = new Font("Courier New", 10); 
      float fontHeight = font.GetHeight(); 
      int startX = 50; 
      int startY = 55; 
      int Offset = 40; 
      graphics.DrawString("TRUE COMFORT HOSTELS. UNOCCUPIED ROOMS", new Font("Courier New", 10), 
           new SolidBrush(Color.Black), startX, startY + Offset); 
      Offset = Offset + 20; 
      graphics.DrawString("============================================================", new Font("Courier New", 8), 
           new SolidBrush(Color.Black), startX, startY + Offset); 
      Offset = Offset + 20; 
      graphics.DrawString("ROOM NUMBER ROOM TYPE", new Font("Courier New", 12), 
           new SolidBrush(Color.Black), startX, startY + Offset); 



       foreach (DataRow dr in tab.Rows) 
       { 


        Offset = Offset + 20; 
        graphics.DrawString(string.Join("      ", dr.ItemArray), new Font("Courier New", 8), 
         new SolidBrush(Color.Black), startX, startY + Offset); 

       if (Offset >= pheight) 
       { 
        e.HasMorePages = true; 
        Offset = 0; 
        return; 
       } 
       else 
       { 
        e.HasMorePages = false; 
       } 





      } 


     } 
    } 
} 
+0

在foreach循環後面添加一個'e.HasMorePages = false;'。 – TaW

回答

0

您需要設置e.HasMorePages =假,當你完成。所以,當你在最後一頁時設置它。

+0

我該怎麼做?請舉例? – mwangii

+0

給我發送所有消息,我可以提供幫助 – Mikes3ds

+0

我已編輯該問題,以包含完整的源代碼 – mwangii