[C#] – 列印DataGridView類

來源:互聯網
上載者:User

來源:http://topic.csdn.net/u/20081111/14/84a390dd-5665-445e-897a-2c728dd865b8.html

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;


/// <summary>
/// <para>列印DataGridView</para>
/// <para>用法:</para>
/// <para>   DataGridViewPrint dgvPrint = new DataGridViewPrint(new DataGridView[] { DataGridView1,DataGridView2 });</para>
/// <para>   dgvPrint.Print();</para>
/// </summary>
public class DataGridViewPrint
{
private DataGridView[] dataGridView;
private PrintDocument printDocument;
private PageSetupDialog pageSetupDialog;
private PrintPreviewDialog printPreviewDialog;

private int dgvIndex = 0;

private int rowCount = 0;
private int colCount = 0;
private int x = 0;
private int y = 0;
int i = 0;

private int rowGap = 60;
private int leftMargin = 50;
private Font font = new Font("Arial", 10);
private Font headingFont = new Font("Arial", 11, FontStyle.Underline);
private Font captionFont = new Font("Arial", 10, FontStyle.Bold);
private Brush brush = new SolidBrush(Color.Black);
private string cellValue = string.Empty;



public DataGridViewPrint(DataGridView[] dataGridView)
{
this.dataGridView = dataGridView;
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}



private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
for (; dgvIndex < dataGridView.Length; dgvIndex++)
{



rowCount = dataGridView[dgvIndex].Rows.Count - 1;

colCount = dataGridView[dgvIndex].ColumnCount;

//print headings
y += rowGap;
x = leftMargin;
for (int j = 0; j < colCount; j++)
{
if (dataGridView[dgvIndex].Columns[j].Width > 0)
{
cellValue = dataGridView[dgvIndex].Columns[j].HeaderText;
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), x, y, dataGridView[dgvIndex].Columns[j].Width, rowGap);
e.Graphics.DrawRectangle(Pens.Black, x, y, dataGridView[dgvIndex].Columns[j].Width, rowGap);
e.Graphics.DrawString(cellValue, headingFont, brush, x, y);
x += dataGridView[dgvIndex].Columns[j].Width;
}
}
//print all rows
for (; i < rowCount; i++)
{
y += rowGap;
x = leftMargin;
for (int j = 0; j < colCount; j++)
{
if (dataGridView[dgvIndex].Columns[j].Width > 0)
{
cellValue = dataGridView[dgvIndex].Rows[i].Cells[j].Value.ToString();
e.Graphics.DrawRectangle(Pens.Black, x, y, dataGridView[dgvIndex].Columns[j].Width, rowGap);
e.Graphics.DrawString(cellValue, font, brush, x, y);
x += dataGridView[dgvIndex].Columns[j].Width;
}
}
if (y >= e.PageBounds.Height - 80)
{
// 允許多頁列印
y = 0;
e.HasMorePages = true;
i++;

return;
}
}
y += rowGap;
for (int j = 0; j < colCount; j++)
{
e.Graphics.DrawString(" ", font, brush, x, y);
}
i = 0;

}
e.HasMorePages = false;
}


public PrintDocument GetPrintDocument()
{
return printDocument;
}

public void Print()
{
try
{
pageSetupDialog = new PageSetupDialog();
pageSetupDialog.Document = printDocument;
pageSetupDialog.ShowDialog();
printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument;
printPreviewDialog.Height = 600;
printPreviewDialog.Width = 800;
printPreviewDialog.ShowDialog();
}
catch (Exception e)
{
throw new Exception("Printer error." + e.Message);
}
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.