Hello
I want to print some data that appear in Form.
I print some data( image, labels....) then at point y = 415, I will print all rows of datagridview, at here all is right, but when i want to print rows that need more page, i had wrong result, this is my trying:

int y = 415;
 for (int i = 0; i < DataGridView1.Rows.Count; i++)
    {
      e.Graphics.DrawLine( Pen, new Point(818, y), new Point(818, y + 35));
      e.Graphics.DrawString(DataGridView1.Rows[i].Cells[0].Value.ToString(), new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Point(770, y + 7), format1);
     e.Graphics.DrawLine( Pen, new Point(621, y), new Point(621, y + 35));
     e.Graphics.DrawString(DataGridView1.Rows[i].Cells[1].Value.ToString(), new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Point(530, y + 7), format1);
      e.Graphics.DrawLine(Pen, new Point(424, y), new Point(424, y + 35));
      e.Graphics.DrawString(DataGridView1.Rows[i].Cells[2].Value.ToString(), new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Point(360, y + 7), format1);
     e.Graphics.DrawLine(new Pen(Color.Black, 3), new Point(227, y), new Point(227, y + 35));
      e.Graphics.DrawString(DataGridView1.Rows[i].Cells[3].Value.ToString(), new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Point(150, y + 7), format1);
      e.Graphics.DrawLine(Pen, new Point(30, y), new Point(30, y + 35));
      e.Graphics.DrawLine(new Pen(Color.Black, 3), new Point(30, y + 35), new Point(818, y + 35));
                y += 35;
                if (y > e.PageBounds.Height - 25)
                { y = 50;   e.HasMorePages = true;     return;   }
                else
                {            e.HasMorePages = false;           }
            } 

Now, if result need more than one page, i get one page duplicated infinite of times, or i get all results in one page(above of each) if i remove syntax "return;" in if(condittion).
thank you

hello, i found my error, I must declare (int i=0) befor its method decleration.
thank you

int currentPage = 1;
int rowsPerPage = 35; //Number of rows per page
int rowIndex = 0;

void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    try
    {
        int x = 5, y = 5;
        int rowMargin = 0;

        int rowCount = dgvHesaab.Rows.Count;
        int pageCount = (int)Math.Ceiling((double)(rowCount / rowsPerPage));

        for (int j = this.dgvHesaab.Columns.Count - 1; j >= 0; j--)
        {
            Rectangle rect = new Rectangle(x, y, 132, dgvHesaab.Rows[0].Height); //132 = Column width in row
            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
            e.Graphics.FillRectangle(Brushes.LightGray, rect);
            e.Graphics.DrawRectangle(Pens.Black, rect);
            if (this.dgvHesaab.Columns[j].HeaderText != null)
            {
                e.Graphics.DrawString(this.dgvHesaab.Columns[j].HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect, sf);
            }
            x += rect.Width;
        }

        x = 5;
        y += dgvHesaab.Rows[0].Height + rowMargin;
        while (rowIndex < dgvHesaab.Rows.Count)
        {
            DataGridViewRow row = dgvHesaab.Rows[rowIndex];
            if (row.Cells[0].Value != null)
            {
                for (int j = this.dgvHesaab.Columns.Count - 1; j >= 0; j--)
                {
                    DataGridViewCell cell = row.Cells[j];
                    Rectangle rect = new Rectangle(x, y, 132, dgvHesaab.Rows[0].Height); //132 = Column width in row
                    StringFormat sf = new StringFormat();
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Center;
                    e.Graphics.DrawRectangle(Pens.Black, rect);
                    if (cell.Value != null)
                    {
                        e.Graphics.DrawString(cell.Value.ToString(), new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, rect, sf);
                    }
                    x += rect.Width;
                }
                x = 5;
                y += dgvHesaab.Rows[0].Height + rowMargin;
            }
            rowIndex++;

            if (rowIndex % rowsPerPage == 0 || rowIndex == dgvHesaab.Rows.Count)
            {
                e.HasMorePages = (currentPage < pageCount);
                currentPage++;
                return;
            }
        }

        dgvHesaab.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        dgvHesaab.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

        dgvHesaab.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        dgvHesaab.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; //Text with more lines in a row.
        dgvHesaab.ColumnHeadersHeight = 40; //40=row height. Text with more lines in a row.

        dgvHesaab.RightToLeft = RightToLeft.Yes;
    }
    catch (Exception)
    {

    }
}


private void btnAdd_Click(object sender, EventArgs e)
{
    try
    {
        //Begin , Adding a row to the gridView(dgvHesaab) 
        ///<summary>
        ///For example, the name of our Grid View is dgvHesaab
        ///Retrieving data from a database or manually inserting data for a row. 
        ///The grade view we have consists of 6 columns. Since the number of rows is small, pagination is not performed, but you can add more rows or read from the database to see the pagination result when printing
        ///</summary>

        //add row 1
        dgvHesaab.Rows.Add(new object[]
        {
            "Row 1 : Item 1",
            "Row 1 : Item 2",
            "Row 1 : Item 3",
            "Row 1 : Item 4",
            "Row 1 : Item 5",
            "Row 1 : Item 6",
        });

        //add row 2
        dgvHesaab.Rows.Add(new object[]
        {
            "Row 2 : Item 1",
            "Row 2 : Item 2",
            "Row 2 : Item 3",
            "Row 2 : Item 4",
            "Row 2 : Item 5",
            "Row 2 : Item 6",
        });
        //End , Adding a row to the gridView(dgvHesaab)  

        dgvHesaab.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        dgvHesaab.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

        dgvHesaab.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        dgvHesaab.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; //Text with more lines in a row.
        dgvHesaab.ColumnHeadersHeight = 40; //40=row height. Text with more lines in a row

        dgvHesaab.RightToLeft = RightToLeft.Yes;

        //Begin Print
        PrintDialog pDialog = new PrintDialog();
        pDialog.AllowCurrentPage = false;
        pDialog.AllowPrintToFile = false;
        pDialog.AllowSelection = false;
        pDialog.AllowSomePages = false;
        pDialog.PrintToFile = false;
        pDialog.ShowHelp = false;
        pDialog.ShowNetwork = false;
        if (pDialog.ShowDialog() == DialogResult.OK)
        {
            System.Drawing.Printing.PrintDocument pdoc = new System.Drawing.Printing.PrintDocument();
            pdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pdoc_PrintPage);
            pdoc.DocumentName = "Title Print";
            pdoc.PrinterSettings = pDialog.PrinterSettings;
            pdoc.DefaultPageSettings = pDialog.PrinterSettings.DefaultPageSettings;
            pdoc.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(100, 100, 100, 100); 
            pdoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169); //PaperSize("A5", 413, 584); OR //PaperSize("A5", 583, 827);
            pdoc.DefaultPageSettings.Landscape = false; //Landscape
            //pdoc.PrinterSettings.Copies = 3 //copy counter
            PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
            printPreviewDialog1.Document = pdoc;
            printPreviewDialog1.ShowDialog();
        }
        //End Print
    }
    catch (Exception)
    {

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.