0
我試圖打印DataGridView,而打印機只打印白頁。 我知道datagridview不是空的,因爲它出現在窗體上。 我也試着用PrintPreviewDialog做它,但它的顯示也是一個白頁。 代碼是這樣的,我不知道什麼是錯的。C#PrintDocument只打印空白頁
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace Prueba
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void imprimirBtn_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font printFont = new Font("Arial", 10);
float topMargin = e.MarginBounds.Top;
float yPos = 0;
float linesPerPage = 0;
int count = 0;
string texto = "";
int i = -1;
DataGridViewRow row;
linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics);
while (count < linesPerPage && i < this.dataGridView1.Rows.Count)
{
row = dataGridView1.Rows[i];
texto = "";
foreach (DataGridViewCell celda in row.Cells)
{
texto += "\t" + celda.Value.ToString();
}
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
e.Graphics.DrawString(texto, printFont, Brushes.Black, 10, yPos);
count++;
i++;
if (i < this.dataGridView1.Rows.Count)
e.HasMorePages = true;
else
{
i = 0;
}
}
}
}
請調試您的代碼。 – Leri