好吧,我有一个打印DataGridView
的系统。在某些情况下,DataGridView
超出了页面的限制,我丢失了最后一行,我不能丢失这些行。我已经尝试使用e.HasMorePages
,但是我得到了无限循环的打印页面...这是我的代码,有些东西是葡萄牙语,这是因为我是巴西人。
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
string header = label1.Text;
Graphics g = ev.Graphics;
Font font = new Font("Times New Roman", 10, FontStyle.Regular);
Pen pen = new Pen(Color.Black);
float fontHeigth = font.GetHeight();
int i = 1;
Pen pe = new Pen(Color.Black, 1);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (i < dataGridView1.Rows.Count)
{
g.DrawLine(pe, 10, startY + fontHeigth + 2 + offset, 779, startY + fontHeigth + 2 + offset);
}
g.DrawString(Convert.ToString(row.Cells["day"].Value), font, new SolidBrush(Color.Black), startX + 2, startY + offset);
offset = offset + (int)fontHeigth + 5;
offsettotal = offsettotal + (int)fontHeigth + 5;
i++;
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
g.DrawString(Convert.ToString(row.Cells["hist"].Value), font, new SolidBrush(Color.Black), 110, startY + offset1);
offset1 = offset1 + (int)fontHeigth + 5;
offsettotal = offsettotal + (int)fontHeigth + 5;
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
g.DrawString(Convert.ToString(row.Cells["descricao"].Value), font, new SolidBrush(Color.Black), 340, startY + offset2);
offset2 = offset2 + (int)fontHeigth + 5;
offsettotal = offsettotal + (int)fontHeigth + 5;
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
g.DrawString(Convert.ToString(row.Cells["empresa"].Value), font, new SolidBrush(Color.Black), 480, startY + offset3);
offset3 = offset3 + (int)fontHeigth + 5;
offsettotal = offsettotal + (int)fontHeigth + 5;
}
double total = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string valor = Convert.ToString(row.Cells["valor"].Value);
Double value;
if (Double.TryParse(valor, out value))
valor = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", value);
else
valor = String.Empty;
g.DrawString(valor, font, new SolidBrush(Color.Black), 670, startY + offset4);
offset4 = offset4 + (int)fontHeigth + 5;
offsettotal = offsettotal + (int)fontHeigth + 5;
total = total + value;
}
string tot = total.ToString();
Double value1;
if (Double.TryParse(tot, out value1))
tot = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", value1);
else
tot = String.Empty;
g.DrawString("Total de créditos: ", new Font("Times New Roman", 11, FontStyle.Underline), new SolidBrush(Color.Black), 550, startY + offset - 20);
g.DrawString(tot, new Font("Times New Roman", 11, FontStyle.Underline), new SolidBrush(Color.Black), 670, startY + offset - 20);
g.DrawLine(new Pen(Color.Black), 10, startY + 24 + offset, 779, startY + 24 + offset);
Image img = Properties.Resources.logo_ropy_novo;
img = resizeImage(img, new Size(250, 80));
if (!(offset > ev.MarginBounds.Bottom))
{
g.DrawImage(img, new Point(10, 10));
g.DrawString(header, new Font("Calibri", 10), new SolidBrush(Color.Black), 425, 10, stringFormat);
}
Font fnt = new Font("Calibri", 14);
Pen p = new Pen(Color.Black, 4);
p.StartCap = LineCap.Round;
p.EndCap = LineCap.ArrowAnchor;
g.DrawLine(p, 10, 125, 22, 125);
g.DrawString("Créditos", new Font("Times New Roman", 15, FontStyle.Italic), new SolidBrush(Color.Black), 24, 114);
g.DrawLine(new Pen(Color.Black), 10, 137, 779, 137);
g.DrawString("Dia", new Font("Times New Roman", 14), new SolidBrush(Color.Black), 10, 140);
g.DrawString("Histórico", new Font("Times New Roman", 13), new SolidBrush(Color.Black), 110, 140);
g.DrawString("Descrição", new Font("Times New Roman", 13), new SolidBrush(Color.Black), 340, 140);
g.DrawString("Empresa", new Font("Times New Roman", 13), new SolidBrush(Color.Black), 480, 140);
g.DrawLine(new Pen(Color.Black), 108, 137, 108, startY + offset - fontHeigth - 7);
g.DrawLine(new Pen(Color.Black), 338, 137, 338, startY + offset - fontHeigth - 7);
g.DrawLine(new Pen(Color.Black), 478, 137, 478, startY + offset - fontHeigth - 7);
g.DrawLine(new Pen(Color.Black), 10, 137, 10, startY + offset - fontHeigth - 7);
g.DrawLine(new Pen(Color.Black), 668, 137, 668, startY + offset);
g.DrawLine(new Pen(Color.Black), 779, 137, 779, startY + offset);
g.DrawString("Valor", new Font("Times New Roman", 13), new SolidBrush(Color.Black), 670, 140);
int y = startY + offset;
g.DrawLine(p, 10, y + 12, 22, y + 12);
g.DrawString("Débitos", new Font("Times New Roman", 15, FontStyle.Italic), new SolidBrush(Color.Black), 24, y);
g.DrawLine(new Pen(Color.Black), 668, y, 779, y);
g.DrawString("Dia", new Font("Times New Roman", 14), new SolidBrush(Color.Black), 10, y + 26);
g.DrawString("Histórico", new Font("Times New Roman", 13), new SolidBrush(Color.Black), 110, y + 26);
g.DrawString("Descrição", new Font("Times New Roman", 13), new SolidBrush(Color.Black), 340, y + 26);
g.DrawString("Empresa", new Font("Times New Roman", 13), new SolidBrush(Color.Black), 480, y + 26);
p.Dispose();
g.DrawString("Valor", new Font("Times New Roman", 13), new SolidBrush(Color.Black), 670, y + 25);
fontHeigth = font.GetHeight();
startX = 10;
startY = 135 + offset + 23;
offset = 40;
offset1 = 40;
offset2 = 40;
offset3 = 40;
offset4 = 40;
i = 1;
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (i < dataGridView2.Rows.Count)
{
g.DrawLine(pe, 10, startY + fontHeigth + 2 + offset, 779, startY + fontHeigth + 2 + offset);
}
g.DrawString(Convert.ToString(row.Cells["day"].Value), font, new SolidBrush(Color.Black), startX + 2, startY + offset);
offset = offset + (int)fontHeigth + 5;
offsettotal = offsettotal + (int)fontHeigth + 5;
i++;
}
foreach (DataGridViewRow row in dataGridView2.Rows)
{
g.DrawString(Convert.ToString(row.Cells["hist"].Value), font, new SolidBrush(Color.Black), 110, startY + offset1);
offset1 = offset1 + (int)fontHeigth + 5;
offsettotal = offsettotal + (int)fontHeigth + 5;
}
foreach (DataGridViewRow row in dataGridView2.Rows)
{
g.DrawString(Convert.ToString(row.Cells["descricao"].Value), font, new SolidBrush(Color.Black), 340, startY + offset2);
offset2 = offset2 + (int)fontHeigth + 5;
offsettotal = offsettotal + (int)fontHeigth + 5;
}
foreach (DataGridViewRow row in dataGridView2.Rows)
{
g.DrawString(Convert.ToString(row.Cells["empresa"].Value), font, new SolidBrush(Color.Black), 480, startY + offset3);
offset3 = offset3 + (int)fontHeigth + 5;
offsettotal = offsettotal + (int)fontHeigth + 5;
}
double totaldb = 0;
foreach (DataGridViewRow row in dataGridView2.Rows)
{
string valor = Convert.ToString(row.Cells["valor"].Value);
Double value;
if (Double.TryParse(valor, out value))
valor = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", value);
else
valor = String.Empty;
g.DrawString(valor, font, new SolidBrush(Color.Black), 670, startY + offset4);
offset4 = offset4 + (int)fontHeigth + 5;
offsettotal = offsettotal + (int)fontHeigth + 5;
totaldb = totaldb + value;
}
string totdb = totaldb.ToString();
Double valuedb;
if (Double.TryParse(totdb, out valuedb))
totdb = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", valuedb);
else
totdb = String.Empty;
g.DrawLine(new Pen(Color.Black), 10, startY + 1, 10, startY + offset - 23);
g.DrawLine(new Pen(Color.Black), 108, startY + 1, 108, startY + offset - 23);
g.DrawLine(new Pen(Color.Black), 338, startY + 1, 338, startY + offset - 23);
g.DrawLine(new Pen(Color.Black), 478, startY + 1, 478, startY + offset - 23);
g.DrawLine(new Pen(Color.Black), 668, startY + 1, 668, startY + offset);
g.DrawLine(new Pen(Color.Black), 779, startY + 1, 779, startY + offset);
g.DrawString("Total de débitos: ", new Font("Times New Roman", 11, FontStyle.Underline), new SolidBrush(Color.Black), 550, startY + offset - 20);
g.DrawString(totdb, new Font("Times New Roman", 11, FontStyle.Underline), new SolidBrush(Color.Black), 670, startY + offset - 20);
g.DrawLine(new Pen(Color.Black), 668, offset + startY, 779, offset + startY);
}
这是OneNote中的实际页面: http://prntscr.com/2ya7xd
答案 0 :(得分:0)
ev.MarginBounds.Height
为您提供边距内页面的高度。 DataGridViewRow.Height
为您提供单独的行高。使用这些信息,您可以检查在每页打印期间是否需要更多页面。
同时逐行绘制可以让您更好地控制打印。有关详细信息,请参阅this文章。
示例代码:
有班级变量int rowIndex=0;
int rowTop=ev.MarginBounds.Top;
bool needMorePages=false;
while (rowIndex<= dataGridView1.Rows.Count - 1)
{
DataGridViewRow row = dataGridView1.Rows[rowIndex];
if(rowTop + row.Height >= ev.MarginBounds.Top + ev.MarginBounds.Height)
{
needMorePages = true;
break;
}
foreach (DataGridViewCell cell in row.Cells)
{
//draw cell content
}
rowTop += row.Height;
rowIndex++;
}
ev.HasMorePages = needMorePages;