打印文档刷新问题

时间:2018-08-02 05:49:17

标签: c# printing windows-forms-designer printdocument

所以我正在编写代码,以使用基于C#表单的控件PRINT DOCUMENT打印报告。我面临的问题是第一次打印完美,但是如果我要再查询一次,代码在e.DrawImage行会出错。但是,如果我关闭我的应用程序并重新开始,它将为我提供完美的第一张照片。如何克服这个问题?

 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        //Start
        Image image = Resources.Final_without_lines;
        e.Graphics.DrawImage(image, 10, 0, 200, 200);

        e.Graphics.DrawString("Printed Date:      " + DateTime.Now, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(540, 10));
        //e.Graphics.DrawString("" + this.comboBox1.Text.Trim(), new Font("Arial", 18, FontStyle.Bold), Brushes.DarkBlue, new Point(320, 30));
        e.Graphics.DrawString("Sales Report", new Font("Arial", 14, FontStyle.Bold), Brushes.SeaGreen, new Point(360, 180));
        e.Graphics.DrawString("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(0, 200));

        e.Graphics.DrawString("Date From : " + theDateCustom + " to  " + theDate2Custom, new Font("Arial", 14, FontStyle.Regular), Brushes.Black, new Point(270, 220));

        e.Graphics.DrawString("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(0, 245));
        e.Graphics.DrawString("S.No", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(10, 270));
        e.Graphics.DrawString("Date", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(140, 270));
        e.Graphics.DrawString("Bill No", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(400, 270));
        e.Graphics.DrawString("Amount", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(600, 270));
        e.Graphics.DrawString("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(0, 300));

       int j = 335;
       int c = 250;
        while (totalnumber < dataGridView1.RowCount - 1)
        {
            e.Graphics.DrawString(counter.ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(10, j));
            DateTime Received;
            DateTime.TryParse(dataGridView1.Rows[i].Cells[1].Value.ToString(), out Received);
            e.Graphics.DrawString(Received.ToString("d"), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(140, j));
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].Value.ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(400, j));
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].Value.ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(600, j));
            //e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].Value.ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(760, j));
            j = j + 30;
            i++;
            counter++;

            totalnumber++;
            if (itemperpage < 24) // check whether  the number of item(per page) is more than 20 or not
            {
                itemperpage += 1; // increment itemperpage by 1
                e.HasMorePages = false; // set the HasMorePages property to false , so that no other page will not be added
            }
            else // if the number of item(per page) is more than 20 then add one page
            {
                itemperpage = 0; //initiate itemperpage to 0 .
                e.HasMorePages = true; //e.HasMorePages raised the PrintPage event once per page .
                return;//It will call PrintPage event again
            }
        }
        //e.HasMorePages = false;
        e.Graphics.DrawString("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(0, j));
        //e.Graphics.DrawString("Total Amount : Rs " + pro.ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(550, j + 30));

        Pen pen = new Pen(Color.Maroon, 2);
        //total amount box
        e.Graphics.DrawRectangle(pen, 590, j + 30, 150, 45);//debit box
        e.Graphics.DrawString(pro.ToString(), new Font("Times New Roman", 20, FontStyle.Bold), Brushes.SeaGreen, new Rectangle(610, j + 35, 75, 40));
        e.Graphics.DrawString("Powered By :Khawaja Tech", new Font("Arial", 12, FontStyle.Bold), Brushes.DarkBlue, new Point(280, j + 100));
        e.Graphics.DrawString("0334-5521065,0312-9081883", new Font("Arial", 12, FontStyle.Bold), Brushes.DarkBlue, new Point(280, j + 130));
        //Here
    }

在datagridview中获取数据的函数

public void fake()
    {
        try
        {
            Connectivity s = new Connectivity();
            //SqlConnection con = new SqlConnection(con1);

            SqlCommand sql = s.sqlCon.CreateCommand();
            //MySqlCommand sql = a.CreateCommand();
            sql.CommandText = "select * from Sale where Date between'" + theDate + "' and '" + theDate2 + "'  ";
            SqlDataAdapter da = new SqlDataAdapter(sql);
            DataSet ds = new DataSet();
            try
            {
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
                //ds.Tables[0]
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unexpected Exception occured. Please try again");
            }
            s.sqlCon.Close();
        }
        catch (Exception ex1)
        {
            MessageBox.Show("Connection to MYSQL databasse lost");
        }

        for (int i = 0; i < dataGridView1.RowCount - 1; i++)
        {
            pro = pro + double.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString());
        }
    }

在这里我正在调用函数

 private void bunifuFlatButton1_Click(object sender, EventArgs e)
     {
        fake();
        Print_Bill(dataGridView1);
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();            
    }

0 个答案:

没有答案