只需在C#中使用Win窗體。創建數據網格視圖並添加打印按鈕。這是我的打印按鈕代碼。如何在多個頁面上打印大型dataGridView/Form?
private void toolStripButton1_Click(object sender, EventArgs e)
{
image = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
this.dataGridView1.DrawToBitmap(image, new Rectangle(new Point(), this.dataGridView1.Size));
memStream = new System.IO.MemoryStream();
printPDialog = new PrintPreviewDialog();
try
{
printFont = new Font("Arial", 10);
image.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp);
image.Save("d:\\Adnan.bmp");
p = new PrintDocument();
p.PrintPage += this.p_PrintPage;
this.printPDialog.Document = p;
printPDialog.Show();
}
catch (System.Runtime.InteropServices.ExternalException ee)
{
}
}
private void p_PrintPage(object sender, PrintPageEventArgs e)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
int? line = null;
// Calculate the number of lines per page.
linesPerPage = e.MarginBounds.Height/
printFont.GetHeight(e.Graphics);
e.Graphics.DrawImage(Image.FromStream(memStream), leftMargin, topMargin);
}
private void printPDialog_FormClosed(object sender, FormClosedEventArgs e)
{
image.Dispose();
memStream.Dispose();
}
問題是我的DataGridView有一個大的列表。我在FlowLayoutPanel裏面有DataGridView。我試圖打印FlowLayoutPanel以及GridView,但在任何一種情況下,垂直滾動條遮蓋的數據都不會被打印。花了幾個小時在線並嘗試使用VB功能包後,我無所適從。
以下是數據在GridView中的外觀。
的問題是數據網格視圖大小。當我創建位圖時,datagridview大小顯示爲1142 * 633屏幕上可見的物理大小。我現在正在嘗試獲取datagridview的總高度,而不僅僅是物理高度,然後嘗試將其傳遞給位圖圖像。 – MStp 2012-02-28 21:15:32
檢查此StackOverFlow發佈,並按照個人已在鏈接上突出顯示的建議http://stackoverflow.com/questions/4530136/how-can-i-print-data-from-a-datagridview-in-c – MethodMan 2012-02-28 21:16:11