-1

I am having trouble getting the content of this datatable to span several pages. It works fine on one page, but if I use e.HasMorePages, it generates infinite print previews. Any assistance will be highly appreciated

              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;
                }





            }


        }
    }
}
mwangii
  • 11
  • 2

1 Answers1

0

You need to set e.HasMorePages = False when you are done. So set it when you are at the last page.

Mikes3ds
  • 592
  • 5
  • 12
  • How would I do that? Example please? – mwangii Apr 08 '16 at 03:01
  • Send me all the source, and I can help – Mikes3ds Apr 08 '16 at 03:08
  • I have edited the question to include the full source code – mwangii Apr 08 '16 at 04:09
  • Thank you. Don't have time tonight to go through your code but a good example is here. From the look of it you need to call false to e.HasMorePages = false; at the last page. [link]https://msdn.microsoft.com/en-us/library/cwbe712d.aspx and [link]http://stackoverflow.com/questions/5018135/automatically-word-wrapping-text-to-a-print-page – Mikes3ds Apr 08 '16 at 04:20
  • Thank you for your help. I finally gave up on this method and decided to use Itextsharp to print out my report. It now works like a charm. – mwangii Apr 11 '16 at 11:01
  • I am glad to hear you have found a solution! Sorry I could not of been more help. – Mikes3ds Apr 11 '16 at 15:03