I need help with printing from my program.
I have written a program in C# that accesses a database, displays the data to a data grid view and then by clicking a button the program allows the data to print what's in the data grid. The first time I print everything prints fine, but when I print a second time I only get the last page of data. This also happens in print preview as well. What am I doing wrong and how can I fix this problem?

Vimal KandasamyRank #2,151 · 661 reputationPosted Apr 30, 2009, 10:16 PM EDT
you have to select coding part and choose "Formatted" option in drop down list in editor window while posting. code will be displayed in right format like given below. private void printToolButton_Click(object sender, EventArgs e) { try { PageSetupDialog setupDlg = new PageSetupDialog(); PrintDialog printDlg = new PrintDialog(); PrintDocument printDoc = new PrintDocument(); printDoc.DocumentName = "Mr. Scanner's Workable Database Report"; printDlg.Document = printDoc; if (printDialog.ShowDialog() == DialogResult.OK) { headerLinesPrinted = 0; recordsFound = 0; pageNumber = 1; lastRow = 0; printDoc.Print(); printDoc.Dispose(); } } catch (Exception ex) { MessageBox.Show("There was an error printing the report\r\n" + ex.Message); } } private void pD_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Font headerFont = new Font("Arial Narrow", 12); Graphics g = e.Graphics; PrintReportHeaders(headerFont, g, e); pageNumber++; } private void PrintReportHeaders(Font headerFont, Graphics g, PrintPageEventArgs e) { int columnPosition = 0; int rowPosition = 0; float headerHeight = headerFont.GetHeight(e.Graphics); string strReportTitle = "Mr. Scanner's Workable Database"; Rectangle rect = new Rectangle(columnPosition, rowPosition, (int)(e.MarginBounds.Width), (int)(headerHeight)); // We retrieve the number of records found from the database for the report. DataSet ds = (DataSet)((BindingSource)dataGridView.DataSource).DataSource; recordsFound = ds.Tables[0].Rows.Count; // These handle our report headers formatting. StringFormat titleHeaderFormat = new StringFormat(); StringFormat dateHeaderFormat = new StringFormat(); StringFormat timeHeaderFormat = new StringFormat(); StringFormat pageNumberFormat = new StringFormat(); StringFormat recordCountFormat = new StringFormat(); titleHeaderFormat.Alignment = StringAlignment.Center; dateHeaderFormat.Alignment = StringAlignment.Near; pageNumberFormat.Alignment = StringAlignment.Far; timeHeaderFormat.Alignment = StringAlignment.Near; recordCountFormat.Alignment = StringAlignment.Far; // Print centered report headers. g.DrawString(strReportTitle, headerFont, Brushes.Black, rect, titleHeaderFormat); rect.Y += (int)headerHeight * 2; g.DrawString("Report Date: " + DateTime.Today.ToShortDateString(), headerFont, Brushes.Black, rect, dateHeaderFormat); g.DrawString(String.Format("Page No.: {0, 7}", pageNumber), headerFont, Brushes.Black, rect, pageNumberFormat); rect.Y += (int)headerHeight; g.DrawString("Report Time: " + DateTime.Now.ToShortTimeString(), headerFont, Brushes.Black, rect, timeHeaderFormat); g.DrawString(String.Format("Records: {0, 7}", recordsFound), headerFont, Brushes.Black, rect, recordCountFormat); rect.Y += (int)headerHeight; rowPosition = rect.Y; PrintColumnHeaders(headerFont, g, ref columnPosition, ref rowPosition, e); rowPosition += (int)headerHeight; headerLinesPrinted = rowPosition; PrintBody(g, ref columnPosition, ref rowPosition, e); } private int PrintColumnHeaders(Font headerFont, Graphics g, ref int columnPosition, ref int rowPosition, PrintPageEventArgs e) { g.DrawLine(Pens.Black, new Point(0, rowPosition), new Point(e.MarginBounds.Width, rowPosition)); foreach (DataGridViewColumn dc in dataGridView.Columns) { g.DrawString(dc.HeaderText, headerFont, Brushes.Black, (float)columnPosition, (float)rowPosition); columnPosition += dc.Width; } rowPosition += (int)headerFont.GetHeight() + 3; g.DrawLine(Pens.Black, new Point(0, rowPosition), new Point(e.MarginBounds.Width, rowPosition)); return columnPosition; } private void PrintBody(Graphics g, ref int columnPosition, ref int rowPosition, PrintPageEventArgs e) { DataSet ds = (DataSet)((BindingSource)dataGridView.DataSource).DataSource; int printableArea = e.MarginBounds.Bottom - headerLinesPrinted; for (int count = lastRow; count printableArea) { lastRow = count + 1; e.HasMorePages = true; return; } else { e.HasMorePages = false; } } } Now it will help someone to fix your error.Vimal KandasamyRank #2,151 · 661 reputationPosted Apr 27, 2009, 9:03 PM EDT
Post your code[printing datagridview].it will help someone to fix the problem.
Eric Mann9 reputationPosted May 5, 2009, 8:35 PM EDT