1

i have a database table having more then 300 records i want to print it using PrintDocument/PrintPreview but the problem is only one page is showing in printPreview the remaining records are missing my Code is below please help me.

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
         int valueYPos=80;
         int valueXPos=20
         string classId=1;
            con.SqlQuery("select [RollNo],[Name] from [dbo].[StudentRegistration] where [ClassId]='" 
            + classId + "'");
            con.ExQuery();
            foreach (DataRow item in con.ExQuery().Rows)
            {
                e.Graphics.DrawString("RollNo" +item[0].ToString(), new Font("Arial", 20, 
                        FontStyle.Bold), Brushes.Black, new Point(valueXPos, valueYPos));
                e.Graphics.DrawString("RollNo" +item[1].ToString(), new Font("Arial", 20, 
                     FontStyle.Bold), Brushes.Black, new Point(valueXPos+20, valueYPos));
                valueYPos +=20;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog pPD = new PrintPreviewDialog();
            pPD.Document = printDocument1;
            pPD.ShowDialog();
        }
SamiTareen
  • 11
  • 1

1 Answers1

-1

For print preview dialog see this answer Showing Print Preview in C#

First of all you can not access data like that because it will try to access data when it try to print each page.

            private int pageNo = 1;

            private static void Print_Invoice(object sender, PrintPageEventArgs e)
            {
                int valueYPos=80;
                int valueXPos=20
                string classId=1;
                con.SqlQuery("select [RollNo],[Name] from [dbo].[StudentRegistration] where [ClassId]='"+ classId + "'");
                con.ExQuery();

                switch (pageNo)
                {
                    case 1:
                        //Print some Data
                        e.HasMorePages = true;
                        break;
                    case 2:
                       //Print some Data
                       e.HasMorePages = true;
                       break;
                    case 3:
                       //Print some Data
                       e.HasMorePages = false;
                       break;
                    default:
                       e.HasMorePages = false;
                       break;
                }

                pageNo++;
                
            }

Hope resolved your problem

  • sir i have to print BalanceSheet report where data will be come from multiple tables and in different structures which is impossible to store in one datagridview. – SamiTareen Oct 22 '20 at 20:07
  • Will there be a loop or you will print it line by line, main point is to check your valueYPos if it exceeds page height then set e.HasMorePages = true else set the e.HasMorepages = false, but you can not use foreach loop, and you can use page number counter to print specific data on page 1 and page 2 and page 3 so on – Muhammad Waqas Aziz Oct 23 '20 at 05:08
  • A poor example: [Why is it a bad programming practice to use if/else?](https://www.quora.com/Why-is-it-a-bad-programming-practice-to-use-if-else), [Why is the 'if' statement considered evil?](https://stackoverflow.com/a/1554691/421195) – paulsm4 Oct 23 '20 at 19:52