如何在 C# 中打印多个页面?使用数据网格视图
How to print multiple pages in c#? Using datagridview
代码中没有问题或错误,但我正在尝试打印包含大量数据的销售报告。我只想打印 Datagridview 上显示的内容,因为我有日期、月份和年份的过滤器。有人可以帮我吗?
这是我的:
private void printBtn_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.DefaultPageSettings.Landscape = true;
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 14);
//float fontweight = font.GetHeight();
float pageWidth = e.PageSettings.PrintableArea.Width;
float pageHeight = e.PageSettings.PrintableArea.Height;
float currentY = 60;// declare one variable for height measurement
e.Graphics.DrawString("SONNY INN", font, Brushes.Black, 40, currentY);//this will print one heading/title in every page of the document
currentY += 15;
e.Graphics.DrawString("\r\nPhilippines\r\nContact No: 0945 678 8057\r\n\r\n\r\n", font, Brushes.Black, 40, currentY);
currentY += 15;
string top = "Date: " + DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");
graphics.DrawString(top + "\r\nLegend : RA - Room Accomodation\r\nAO - Add-Ons", font, new SolidBrush(Color.Black), 600, 60);
currentY += 60;
e.Graphics.DrawString("------------------------------------------------------------------------------------", font, Brushes.Black, 40, currentY);
currentY += 30;
e.Graphics.DrawString("Transaction Date\tGuest\t\t\t\tRoom No.\t\tDetails\t\tAmount\t\t\tBalance Due", new Font("Courier New", 10), Brushes.Black, 50, currentY);
currentY += 30;
e.Graphics.DrawString("------------------------------------------------------------------------------------", font, new SolidBrush(Color.Black), 40, 750);
e.Graphics.DrawString("TOTAL AMOUNT: PHP \t\t\t\t\t\t" + totamtxtbox.Text, font, new SolidBrush(Color.Black), 50, 780);
e.Graphics.DrawString("TOTAL BALANCE: PHP \t\t\t\t\t" + balduetxt.Text, font, new SolidBrush(Color.Black), 50, 800);
e.Graphics.DrawString("GRAND TOTAL: PHP \t\t\t\t\t\t" + gtotaltxt.Text, font, new SolidBrush(Color.Black), 50, 820);
int offsetY = 40;
int y = 0;
int cnt = reportgrid.Rows.Count;
foreach (DataGridViewRow dr in reportgrid.Rows)
{
string transd = Convert.ToString(dr.Cells["Transaction Date"].Value);
string guest = Convert.ToString(dr.Cells["Guest"].Value);
string roomno = Convert.ToString(dr.Cells["Room No."].Value);
string deets = Convert.ToString(dr.Cells["Details"].Value);
string amount = Convert.ToString(dr.Cells["Amount"].Value);
string baldue = Convert.ToString(dr.Cells["Balance Due"].Value);
graphics.DrawString(transd + "\t" + guest + "\t\t\t" + roomno + "\t\t\t" + deets + "\t\t\t" + amount + "\t\t\t" + baldue, new Font("Courier New", 10), new SolidBrush(Color.Red), 50, currentY);
currentY += 20;
}
MessageBox.Show("Printing in progress.", "Print Sales Report", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
从 DataGridView 打印多页并不是最简单的事情,因此我建议您使用现有的代码示例之一。这里有几个很好用的,可以打印多页:
代码中没有问题或错误,但我正在尝试打印包含大量数据的销售报告。我只想打印 Datagridview 上显示的内容,因为我有日期、月份和年份的过滤器。有人可以帮我吗?
这是我的:
private void printBtn_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.DefaultPageSettings.Landscape = true;
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 14);
//float fontweight = font.GetHeight();
float pageWidth = e.PageSettings.PrintableArea.Width;
float pageHeight = e.PageSettings.PrintableArea.Height;
float currentY = 60;// declare one variable for height measurement
e.Graphics.DrawString("SONNY INN", font, Brushes.Black, 40, currentY);//this will print one heading/title in every page of the document
currentY += 15;
e.Graphics.DrawString("\r\nPhilippines\r\nContact No: 0945 678 8057\r\n\r\n\r\n", font, Brushes.Black, 40, currentY);
currentY += 15;
string top = "Date: " + DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");
graphics.DrawString(top + "\r\nLegend : RA - Room Accomodation\r\nAO - Add-Ons", font, new SolidBrush(Color.Black), 600, 60);
currentY += 60;
e.Graphics.DrawString("------------------------------------------------------------------------------------", font, Brushes.Black, 40, currentY);
currentY += 30;
e.Graphics.DrawString("Transaction Date\tGuest\t\t\t\tRoom No.\t\tDetails\t\tAmount\t\t\tBalance Due", new Font("Courier New", 10), Brushes.Black, 50, currentY);
currentY += 30;
e.Graphics.DrawString("------------------------------------------------------------------------------------", font, new SolidBrush(Color.Black), 40, 750);
e.Graphics.DrawString("TOTAL AMOUNT: PHP \t\t\t\t\t\t" + totamtxtbox.Text, font, new SolidBrush(Color.Black), 50, 780);
e.Graphics.DrawString("TOTAL BALANCE: PHP \t\t\t\t\t" + balduetxt.Text, font, new SolidBrush(Color.Black), 50, 800);
e.Graphics.DrawString("GRAND TOTAL: PHP \t\t\t\t\t\t" + gtotaltxt.Text, font, new SolidBrush(Color.Black), 50, 820);
int offsetY = 40;
int y = 0;
int cnt = reportgrid.Rows.Count;
foreach (DataGridViewRow dr in reportgrid.Rows)
{
string transd = Convert.ToString(dr.Cells["Transaction Date"].Value);
string guest = Convert.ToString(dr.Cells["Guest"].Value);
string roomno = Convert.ToString(dr.Cells["Room No."].Value);
string deets = Convert.ToString(dr.Cells["Details"].Value);
string amount = Convert.ToString(dr.Cells["Amount"].Value);
string baldue = Convert.ToString(dr.Cells["Balance Due"].Value);
graphics.DrawString(transd + "\t" + guest + "\t\t\t" + roomno + "\t\t\t" + deets + "\t\t\t" + amount + "\t\t\t" + baldue, new Font("Courier New", 10), new SolidBrush(Color.Red), 50, currentY);
currentY += 20;
}
MessageBox.Show("Printing in progress.", "Print Sales Report", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
从 DataGridView 打印多页并不是最简单的事情,因此我建议您使用现有的代码示例之一。这里有几个很好用的,可以打印多页: