2016-04-08 52 views
-1

このデータテーブルの内容を複数のページにまたがるのに問題があります。 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 = Falseに設定する必要があります。したがって、最後のページにいるときに設定してください。

+0

どうすればいいですか?例をお願いしますか? – mwangii

+0

私にすべてのソースを送って、私は助けることができます – Mikes3ds

+0

私は完全なソースコードを含めるように質問を編集しました – mwangii

関連する問題