打印DATAGRIDVIEW 或LISTVIEW 的!
解决方案 »
- Enum.GetValues和Enum.GetNames有什么区别
- 请教,c# Winfrom中的DataGridView行颜色
- WinForm中多控件闪烁问题
- WPF中的TreeView如何得到选中节点的深度?
- 分析器错误
- 请问在C#中如何读取一张光盘的Media ID?
- 各位兄弟请帮帮忙:如何在C#程序中实现插件架构
- DataGrid使用SetDataBinding方法运行时设置数据源,请问什么叫运行时?
- odp.net的oraclehelper
- 试卷组成的问题!
- C#.net里treeview刷新问题 急!急!急!急!
- 我用Process要开Notepad.exe,想让它只开一个,如果打开了,就让它的主窗口在桌面最前面激活??
{
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 = ""; 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);
} }
}}
函数时!!
DataGridViewPrint()的括号里面添什么啊!!