how check if printing content on new page

29 views Asked by At

I have a DataGridView containing data about different customers, and my objective is to print each row on a separate page. However, I'm facing a problem where the data from two consecutive rows prints on the same page, overlapping each other. For example, if I have four rows in my DataGridView, the content of rows 1 and 2 prints on the same page, and similarly for rows 3 and 4.

Below is the relevant code snippet where I handle the printing logic:

private int currentRow = 0;
private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
{
    string companyName = "چوہان ڈیری فارمز"; // Your company name

    DateTime startDate = dtm_start.Value;
    DateTime endDate = dtm_end.Value;

    // Set font and brush for drawing
    Font headingfont = new Font("Times New Roman", 18, FontStyle.Bold);
    Font font = new Font("Times New Roman", 13, FontStyle.Regular);
    Font linefont = new Font("Jameel Noori Nastaleeq", 12, FontStyle.Regular);
    Font infofont = new Font("Times New Roman", 11, FontStyle.Regular);
    Font nofont = new Font("Jameel Noori Nastaleeq", 9, FontStyle.Regular);

    Brush brush = Brushes.Black;

    Pen pen = new Pen(Color.Black, 2);

    // Calculate the width and height of the page
    float pageWidth = e.PageBounds.Width;
    float pageHeight = e.PageBounds.Height;

    // Measure the size of the text
    SizeF textSize = e.Graphics.MeasureString(companyName, headingfont);

    // Calculate X coordinate to center the text
    float x = (pageWidth - textSize.Width) / 2;

    int xAxis = 190;

    
    if(currentRow<dataGridView2.Rows.Count)
    {

        DataGridViewRow row = dataGridView2.Rows[currentRow];

        e.Graphics.DrawString(companyName, headingfont, brush, x, 17); // Adjust the Y coordinate as needed
        e.Graphics.DrawString("--------------------------------------------", linefont, brush, 20, 30); // Adjust the Y coordinate as needed

        e.Graphics.DrawString(":اکاؤنٹ نمبر", font, brush, xAxis, 60);
        e.Graphics.DrawString(":نام", font, brush, xAxis, 90);

        e.Graphics.DrawString("--------------------------------------------", linefont, brush, 20, 100);

        e.Graphics.DrawString(":تاریخ", font, brush, 225, 125);

        e.Graphics.DrawString("--------------------------------------------", linefont, brush, 20, 140);

        e.Graphics.DrawString(":سابقہ بیلنس", font, brush, xAxis, 170);
        e.Graphics.DrawString(":ٹوٹل لیٹر", font, brush, xAxis, 200);
        e.Graphics.DrawString(":دودھ رقم", font, brush, xAxis, 230);
        e.Graphics.DrawString(":پرچی رقم", font, brush, xAxis, 260);
        e.Graphics.DrawString(":بیلنس", font, brush, xAxis, 290);

        //e.Graphics.DrawString(startDate.Date.ToString(), font, brush, 160, 130);

        if (row.Cells["Id"].Value != null)
            e.Graphics.DrawString(row.Cells["Id"].Value.ToString(), font, brush, 150, 60);
        if (row.Cells["Customer Name"].Value != null)
            e.Graphics.DrawString(row.Cells["Customer Name"].Value.ToString(), font, brush, 70, 90);
        if (row.Cells["Previous Balance"].Value != null)
            e.Graphics.DrawString(row.Cells["Previous Balance"].Value.ToString(), font, brush, 70, 170);

        // write status in urdu for previous balance
        if (row.Cells["pStatus"].Value != null)
        {
            string pStatus = row.Cells["pStatus"].Value.ToString();
            if (pStatus == "Credit")
            {
                e.Graphics.DrawString("جمع", font, brush, 40, 170);
            }
            else
            {
                e.Graphics.DrawString("بنام", font, brush, 40, 170);
            }
        }

        if (row.Cells["Total Liters"].Value != null)
            e.Graphics.DrawString(row.Cells["Total Liters"].Value.ToString(), font, brush, 70, 200);
        if (row.Cells["Milk Amount"].Value != null)
            e.Graphics.DrawString(row.Cells["Milk Amount"].Value.ToString(), font, brush, 70, 230);
        if (row.Cells["Parchi Amount"].Value != null)
            e.Graphics.DrawString(row.Cells["Parchi Amount"].Value.ToString(), font, brush, 70, 260);
        if (row.Cells["Closing Balance"].Value != null)
            e.Graphics.DrawString(row.Cells["Closing Balance"].Value.ToString(), font, brush, 70, 290);

        if (row.Cells["Status"].Value != null)
        {
            string cStatus = row.Cells["Status"].Value.ToString();
            if (cStatus == "Credit")
            {
                e.Graphics.DrawString("جمع", font, brush, 40, 290);
            }
            else
            {
                e.Graphics.DrawString("بنام", font, brush, 40, 290);
            }
        }

        e.Graphics.DrawString("--------------------------------------------", linefont, brush, 20, 310);

        e.Graphics.DrawString("کسی بھی غلط حساب کی صورت میں جلد از جلد", infofont, brush, 19, 340);
        e.Graphics.DrawString("ہم سے رابطہ کریں۔ شکریہ", infofont, brush, 130, 365);

        e.Graphics.DrawString("03346565189 :فون نمبر" + " ", infofont, brush, 128, 395);
        e.Graphics.DrawString("آپ کے تعاون کا شکریہ", infofont, brush, 80, 425);


        currentRow++;
        e.HasMorePages = true;

        
    }
    else
    {
        currentRow = 0;
        e.HasMorePages=false;
    }

    
}

Despite my attempts to print each row on a separate page by setting e.HasMorePages to true after printing each row, the issue persists.

0

There are 0 answers