Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am printing invoice using PrintDialogue and PrintDocument. Invoice details are handling using Data Grid View.Data Grid View is iterating using for..each statement.I have already set e.hasMorePages into true.But printing invoice rewriting same pages.Not came in separate pages.Please help me to print in separate pages.


C#
 //Printing Starts Here
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
   // e.HasMorePages = true;
    Graphics graphics = e.Graphics;
    Font font = new Font("Courier New", 10);
    float fontHeight = font.GetHeight();
    int startxsupp = 65;
    int startysupp = 124;
    int offsetsupp = 40;
    //-----------------SUPPLIER NAME------------------------------------------------------------------
    graphics.DrawString(txtSupp.Text, new Font("Courier New", 12, FontStyle.Bold),
                        new SolidBrush(Color.Black), startxsupp, startysupp + offsetsupp);
    offsetsupp = offsetsupp + 80;
    //------------------------------------------------------------------------------------------------
    //-----------------SUPPLIER ADDRESS---------------------------------------------------------------
    int startxsuppadd = 60;
    graphics.DrawString(txtSuppAddress.Text, new Font("Courier New", 12, FontStyle.Bold),
                        new SolidBrush(Color.Black), startxsuppadd, startysupp + offsetsupp);
    //-------------------------------------------------------------
    int startX = 660;
    int startY = 120;
    int Offset = 40;
    //-------------------Invoice Details-------------------------------------------------------------
    graphics.DrawString(txtInvoiceNo.Text, new Font("Courier New", 12, FontStyle.Bold),
                        new SolidBrush(Color.Black), startX, startY + Offset);
    Offset = Offset + 40;
    string formattedInvDate = dtpInvDate.Value.ToString("dd-MM-yyyy");
    graphics.DrawString(formattedInvDate,
             new Font("Courier New", 12, FontStyle.Bold),
             new SolidBrush(Color.Black), startX, startY + Offset);
    Offset = Offset + 35;
    //-----------------------------------------------------------------------------------------------
    graphics.DrawString(txtDONo.Text.ToString(),
             new Font("Courier New", 12, FontStyle.Bold),
             new SolidBrush(Color.Black), startX, startY + Offset);
    Offset = Offset + 35;
    string formattedDoDate = dtpDOdate.Value.ToString("dd-MM-yyyy");
    graphics.DrawString(formattedDoDate,
             new Font("Courier New", 12, FontStyle.Bold),
             new SolidBrush(Color.Black), startX, startY + Offset);
    Offset = Offset + 40;
    //-----------------------------------------------------------------------------------------------
    graphics.DrawString(txtLPONo.Text.ToString(),
             new Font("Courier New", 12, FontStyle.Bold),
             new SolidBrush(Color.Black), startX, startY + Offset);
    Offset = Offset + 40;
    //----------------------------------------------------------------------------------------------
    int startxgrid = 60;
    int startygrid = 360;
    int offsetgridy = 40;
        foreach (DataGridViewRow dr in gridInvDetail.Rows)
        {
            if ((dr.Cells["SLNO"].Value ?? string.Empty).ToString() != null)
            {
                //----------------------------------------------------------------------------------
                string col1 = (dr.Cells["SLNO"].Value ?? string.Empty).ToString();
                string col2 = (dr.Cells["DESC"].Value ?? string.Empty).ToString();
                string col3 = (dr.Cells["Unit"].Value ?? string.Empty).ToString();
                string col4 = (dr.Cells["Quantity"].Value ?? string.Empty).ToString();
                string col5 = (dr.Cells["Rate"].Value ?? string.Empty).ToString();
                string col6 = (dr.Cells["Amount"].Value ?? string.Empty).ToString();
                //--------------------INVOICE ITEMS SERIAL NO---------------------------------------
                startxgrid = 30;
                graphics.DrawString(col1,
                    new Font("Courier New", 12, FontStyle.Bold),
                    new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
                //--------------------INVOICE ITEMS DESCRIPTION------------------------------------
                startxgrid = 60;
                graphics.DrawString(col2,
                    new Font("Courier New", 12, FontStyle.Bold),
                    new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
                //--------------------INVOICE ITEMS UNIT-------------------------------------------
                startxgrid = 510;
                graphics.DrawString(col3,
                    new Font("Courier New", 12, FontStyle.Bold),
                    new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
                //--------------------INVOICE ITEMS QTY--------------------------------------------
                startxgrid = 590;
                graphics.DrawString(col4,
                    new Font("Courier New", 12, FontStyle.Bold),
                    new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
                //--------------------INVOICE ITEMS RATE------------------------------------------
                startxgrid = 655;
                graphics.DrawString(string.Format("{0:#,##0.00}", double.Parse(col5)),
                    new Font("Courier New", 12, FontStyle.Bold),
                    new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
                //--------------------INVOICE ITEMS TOTAL------------------------------------------
                startxgrid = 750;
                graphics.DrawString(string.Format("{0:#,##0.00}", double.Parse(col6)),
                    new Font("Courier New", 12, FontStyle.Bold),
                    new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
                //graphics.DrawString(string.Format((startygrid + offsetgridy).ToString()),
                //        new Font("Courier New", 12, FontStyle.Bold),
                //        new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
                //----------------------------------------------------------------------------------
                offsetgridy = offsetgridy + 20;
                //----------------------------------------------------------------------------------
                if ((startygrid + offsetgridy) > 900)
                {    
                        e.HasMorePages = true;
                        offsetgridy = 40;
                }
            }
            else
            {
                break;
            }
        }
    e.HasMorePages = false;
}
Posted
Updated 25-Sep-14 2:24am
v3
Comments
Richard MacCutchan 25-Sep-14 7:02am    
You need to provide more detail than this, no one can guess what your code is doing.
shafeeqpv 25-Sep-14 7:56am    
Sorry Sir,
I have updates the question with the code.your help will be very helpful to me.
With Regards,
Shafeeq
Richard MacCutchan 25-Sep-14 8:26am    
You are iterating through your gridview from the beginning in each page. You should be setting the row number outside of this code.

1 solution

The PrintPage() event is called once per page until the e.HasMorePages is false - therefore any code in that sub will be executed once per page.

You need to specify (and initialise) your "what have I already printed" outside of this event otherwise it will always keep resetting.

See the article An absolute beginner's guide to printing in .NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900