Fix pagination problem when printing in C# using HasMorePages.
Explanation about this code: You can add many rows or read from the database to see the result of pagination during printing, for example, I added only two rows to the table manually, which is not paginated because the number of rows is small. Add many rows manually or insert information from the database into the grid view. This is an example:
int currentPage = 1;
int rowsPerPage = 36; //Number of rows per page
int rowIndex = 0;
void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
int x = 5, y = 5; //Drawing start position
int rowMargin = 0; //Spacing between lines
dgvHesaab.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgvHesaab.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgvHesaab.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgvHesaab.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; //Text with more lines in a row.
dgvHesaab.ColumnHeadersHeight = 40; //40=row height. Text with more lines in a row.
dgvHesaab.RightToLeft = RightToLeft.Yes;
int rowCount = dgvHesaab.Rows.Count; //Get the number of rows
int pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(rowCount) / Convert.ToDouble(rowsPerPage)));
//Set header content of columns
for (int j = this.dgvHesaab.Columns.Count - 1; j >= 0; j--)
{
Rectangle rect = new Rectangle(x, y, 132, dgvHesaab.Rows[0].Height); //132 = width in row
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.FillRectangle(Brushes.LightGray, rect);
e.Graphics.DrawRectangle(Pens.Black, rect);
if (this.dgvHesaab.Columns[j].HeaderText != null)
{
e.Graphics.DrawString(this.dgvHesaab.Columns[j].HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect, sf);
}
x += rect.Width;
}
x = 5;
y += dgvHesaab.Rows[0].Height + rowMargin;
//Set the content of the lines
while (rowIndex < dgvHesaab.Rows.Count)
{
DataGridViewRow row = dgvHesaab.Rows[rowIndex];
if (row.Cells[0].Value != null)
{
for (int j = this.dgvHesaab.Columns.Count - 1; j >= 0; j--)
{
DataGridViewCell cell = row.Cells[j];
Rectangle rect = new Rectangle(x, y, 132, dgvHesaab.Rows[0].Height); //132 = width in row
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawRectangle(Pens.Black, rect);
if (cell.Value != null)
{
e.Graphics.DrawString(cell.Value.ToString(), new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, rect, sf);
}
x += rect.Width;
}
x = 5;
y += dgvHesaab.Rows[0].Height + rowMargin;
}
rowIndex++;
//Print a new page if necessary
if (rowIndex % rowsPerPage == 0 || rowIndex == dgvHesaab.Rows.Count)
{
e.HasMorePages = (currentPage < pageCount);
currentPage++;
return;
}
}
}
catch (Exception)
{
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
//Begin , Adding a row to the gridView(dgvHesaab)
///<summary>
///For example, the name of our Grid View is dgvHesaab
///Retrieving data from a database or manually inserting data for a row.
///</summary>
//add row 1
dgvHesaab.Rows.Add(new object[]
{
"Row 1 : Item 1",
"Row 1 : Item 2",
"Row 1 : Item 3",
"Row 1 : Item 4",
"Row 1 : Item 5",
"Row 1 : Item 6",
});
//add row 2
dgvHesaab.Rows.Add(new object[]
{
"Row 2 : Item 1",
"Row 2 : Item 2",
"Row 2 : Item 3",
"Row 2 : Item 4",
"Row 2 : Item 5",
"Row 2 : Item 6",
});
//End , Adding a row to the gridView(dgvHesaab)
dgvHesaab.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgvHesaab.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgvHesaab.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgvHesaab.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; //Text with more lines in a row.
dgvHesaab.ColumnHeadersHeight = 40; //40=row height. Text with more lines in a row.
dgvHesaab.RightToLeft = RightToLeft.Yes;
//Begin Print
PrintDialog pDialog = new PrintDialog();
pDialog.AllowCurrentPage = false;
pDialog.AllowPrintToFile = false;
pDialog.AllowSelection = false;
pDialog.AllowSomePages = false;
pDialog.PrintToFile = false;
pDialog.ShowHelp = false;
pDialog.ShowNetwork = false;
if (pDialog.ShowDialog() == DialogResult.OK)
{
System.Drawing.Printing.PrintDocument pdoc = new System.Drawing.Printing.PrintDocument();
pdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pdoc_PrintPage);
pdoc.DocumentName = "Print Title";
pdoc.PrinterSettings = pDialog.PrinterSettings;
pdoc.DefaultPageSettings = pDialog.PrinterSettings.DefaultPageSettings;
pdoc.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(100, 100, 100, 100);
pdoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169);
pdoc.DefaultPageSettings.Landscape = false; //Landscape
//pdoc.PrinterSettings.Copies = 3;
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = pdoc;
pdoc.Print(); //No preview
//printPreviewDialog1.ShowDialog(); //printPreview
}
//End Print
}
catch (Exception)
{
}
}
Fix pagination problem when printing in C# using HasMorePages.
Explanation about this code: You can add many rows or read from the database to see the result of pagination during printing, for example, I added only two rows to the table manually, which is not paginated because the number of rows is small. Add many rows manually or insert information from the database into the grid view. This is an example:
int currentPage = 1;
int rowsPerPage = 36; //Number of rows per page
int rowIndex = 0;
void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
int x = 5, y = 5; //Drawing start position
int rowMargin = 0; //Spacing between lines
dgvHesaab.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgvHesaab.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgvHesaab.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgvHesaab.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; //Text with more lines in a row.
dgvHesaab.ColumnHeadersHeight = 40; //40=row height. Text with more lines in a row.
dgvHesaab.RightToLeft = RightToLeft.Yes;
int rowCount = dgvHesaab.Rows.Count; //Get the number of rows
int pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(rowCount) / Convert.ToDouble(rowsPerPage)));
//Set header content of columns
for (int j = this.dgvHesaab.Columns.Count - 1; j >= 0; j--)
{
Rectangle rect = new Rectangle(x, y, 132, dgvHesaab.Rows[0].Height); //132 = width in row
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.FillRectangle(Brushes.LightGray, rect);
e.Graphics.DrawRectangle(Pens.Black, rect);
if (this.dgvHesaab.Columns[j].HeaderText != null)
{
e.Graphics.DrawString(this.dgvHesaab.Columns[j].HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect, sf);
}
x += rect.Width;
}
x = 5;
y += dgvHesaab.Rows[0].Height + rowMargin;
//Set the content of the lines
while (rowIndex < dgvHesaab.Rows.Count)
{
DataGridViewRow row = dgvHesaab.Rows[rowIndex];
if (row.Cells[0].Value != null)
{
for (int j = this.dgvHesaab.Columns.Count - 1; j >= 0; j--)
{
DataGridViewCell cell = row.Cells[j];
Rectangle rect = new Rectangle(x, y, 132, dgvHesaab.Rows[0].Height); //132 = width in row
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawRectangle(Pens.Black, rect);
if (cell.Value != null)
{
e.Graphics.DrawString(cell.Value.ToString(), new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, rect, sf);
}
x += rect.Width;
}
x = 5;
y += dgvHesaab.Rows[0].Height + rowMargin;
}
rowIndex++;
//Print a new page if necessary
if (rowIndex % rowsPerPage == 0 || rowIndex == dgvHesaab.Rows.Count)
{
e.HasMorePages = (currentPage < pageCount);
currentPage++;
return;
}
}
}
catch (Exception)
{
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
//Begin , Adding a row to the gridView(dgvHesaab)
///<summary>
///For example, the name of our Grid View is dgvHesaab
///Retrieving data from a database or manually inserting data for a row.
///</summary>
//add row 1
dgvHesaab.Rows.Add(new object[]
{
"Row 1 : Item 1",
"Row 1 : Item 2",
"Row 1 : Item 3",
"Row 1 : Item 4",
"Row 1 : Item 5",
"Row 1 : Item 6",
});
//add row 2
dgvHesaab.Rows.Add(new object[]
{
"Row 2 : Item 1",
"Row 2 : Item 2",
"Row 2 : Item 3",
"Row 2 : Item 4",
"Row 2 : Item 5",
"Row 2 : Item 6",
});
//End , Adding a row to the gridView(dgvHesaab)
dgvHesaab.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgvHesaab.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgvHesaab.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgvHesaab.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; //Text with more lines in a row.
dgvHesaab.ColumnHeadersHeight = 40; //40=row height. Text with more lines in a row.
dgvHesaab.RightToLeft = RightToLeft.Yes;
//Begin Print
PrintDialog pDialog = new PrintDialog();
pDialog.AllowCurrentPage = false;
pDialog.AllowPrintToFile = false;
pDialog.AllowSelection = false;
pDialog.AllowSomePages = false;
pDialog.PrintToFile = false;
pDialog.ShowHelp = false;
pDialog.ShowNetwork = false;
if (pDialog.ShowDialog() == DialogResult.OK)
{
System.Drawing.Printing.PrintDocument pdoc = new System.Drawing.Printing.PrintDocument();
pdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pdoc_PrintPage);
pdoc.DocumentName = "Print Title";
pdoc.PrinterSettings = pDialog.PrinterSettings;
pdoc.DefaultPageSettings = pDialog.PrinterSettings.DefaultPageSettings;
pdoc.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(100, 100, 100, 100);
pdoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169);
pdoc.DefaultPageSettings.Landscape = false; //Landscape
//pdoc.PrinterSettings.Copies = 3;
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = pdoc;
pdoc.Print(); //No preview
//printPreviewDialog1.ShowDialog(); //printPreview
}
//End Print
}
catch (Exception)
{
}
}