【发布时间】:2018-10-24 10:58:08
【问题描述】:
大家好,我有一个带有DataGridView 和一些控件的 WinForm。我想打印从这个DataGridView 到PrintDocument 的特定列。我设计了一种带有硬编码标题的账单格式,如 Sr.、Qty、Rate 等。
多页可以有足够的数据,所以我希望这个标题显示在每页的顶部,我还想添加页码,打印所有行后,页面末尾有总计、折扣和应付账款计算我想确保它出现在最后一页的末尾。
编辑:我已经设法从选定的列打印,因为我是学习者,我不知道它是否可以在 A4 之外工作。我想知道Bill Calculation 部分将触发drawDataGridTab2()如果有很多页后,这部分会在同一位置打印在最后一页吗?
如果您能帮助我了解如何在不丢失格式的情况下打印除 A4 以外的内容。
下面我正在粘贴新的账单格式代码。
private void printDocumentTab2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Draw Header
////
Graphics graphic = e.Graphics;
Font font = new Font("Courier New", 12);
float fontHeight = font.GetHeight();
graphic.DrawString(txtSupplierTab2.Text, new Font("Courier New", 20), Brushes.Black, 20, 20);
graphic.DrawString(txtRichAddressTab2.Text, new Font("Courier New", 12), Brushes.Black, 20, 50);
graphic.DrawString(txtCityTab2.Text, new Font("Courier New", 12), Brushes.Black, 20, 105);
graphic.DrawString("Ph: " + txtContactTab2.Text, new Font("Courier New", 12), Brushes.Black, 20, 120);
graphic.DrawLine(new Pen(Color.Blue, 2), 0, 140, this.Width, 140);
//Call method to Draw Headers
drawHeaderTab2(new Font("Courier New", 12, FontStyle.Bold), graphic);
graphic.DrawLine(new Pen(Color.Blue, 2), 0, 165, this.Width, 165);
//Call method to Draw DataGrid
drawDataGridTab2(new Font("Courier New", 10, FontStyle.Bold), graphic);
//Bill Calucation
graphic.DrawLine(new Pen(Color.Blue, 2), 0, 900, this.Width, 900);
//Gross Total at the end of Bill
graphic.DrawString("Total", new Font("Courier New", 12), Brushes.DarkRed, 600, 905);
graphic.DrawString(txtGrossTotal_Tab2.Text, new Font("Courier New", 12), Brushes.DarkRed, 700, 905);
//Discount At the End of Bill
graphic.DrawString("Disc.", new Font("Courier New", 12), Brushes.DarkRed, 600, 925);
graphic.DrawString(txtGrossTotalDisc_Tab2.Text, new Font("Courier New", 12), Brushes.DarkRed, 700, 925);
//Payable at the end of the bill
graphic.DrawLine(new Pen(Color.Black, 1.5f), 600, 945, this.Width, 945);
graphic.DrawString("Payable", new Font("Courier New", 12), Brushes.DarkRed, 600, 945);
graphic.DrawString(lblGrandAmmount_Tab2.Text, new Font("Courier New", 12), Brushes.DarkRed, 700, 945);
}
DrawHeader 函数
private void drawHeaderTab2(Font font, Graphics g)
{
g.DrawString("Sr#", font, Brushes.Black, 20, 145);
string medicine = dataGridView1.Columns["medName"].HeaderText;
g.DrawString(medicine, font, Brushes.Black, 70, 145);
string qty = dataGridView1.Columns["purchasedQty"].HeaderText;
g.DrawString(qty, font, Brushes.Black, 500, 145);
string rate = dataGridView1.Columns["costPrice"].HeaderText;
g.DrawString(rate, font, Brushes.Black, 600, 145);
string amount = dataGridView1.Columns["grossTotal"].HeaderText;
g.DrawString(amount, font, Brushes.Black, 750, 145);
}
DrawDataGridView 函数
private void drawDataGridTab2(Font font, Graphics g)
{
int yCord = 170;
int y1 = 185;
int y2 = 185;
int rows = 1;
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
//g.DrawString(dr.ToString(), new Font("Courier New", 10), Brushes.Black, 10, 150);
g.DrawString(rows.ToString(), font, Brushes.Black, 20, yCord);
string medicine = dataGridView1.Rows[dr.Index].Cells["medName"].FormattedValue.ToString();
g.DrawString(medicine, font, Brushes.Black, 70, yCord);
string qty = dataGridView1.Rows[dr.Index].Cells["purchasedQty"].FormattedValue.ToString();
g.DrawString(qty, font, Brushes.Black, 500, yCord);
string rate = dataGridView1.Rows[dr.Index].Cells["costPrice"].FormattedValue.ToString();
g.DrawString(rate, font, Brushes.Black, 600, yCord);
string amount = dataGridView1.Rows[dr.Index].Cells["grossTotal"].FormattedValue.ToString();
g.DrawString(amount, font, Brushes.Black, 750, yCord);
g.DrawLine(new Pen(Color.Black, 1), 0, y1, this.Width, y2);
yCord += 15;
y1 += 15;
y2 += 15;
rows++;
}
}
【问题讨论】:
-
您是否尝试过使用 ReportViewer?
-
还没有。你能给我介绍一下关于 Report Viewer 的好文档吗? ?
-
您可以计算要打印的总行数,并根据打印行设置 PrintPageEventArgs.HasMorePages true/false。 Reference
-
我已经更新了代码和问题。请检查
标签: c# .net winforms datagridview printdocument