C# Multiple Page Printing
<p>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</p>

<pre><code>              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 &gt;= pheight)
                {
                    e.HasMorePages = true;
                    Offset = 0;
                    return;
                }
                else
                {
                    e.HasMorePages = false;
                }





            }


        }
    }
}
</code></pre>