博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[C#] - 打印DataGridView类
阅读量:5307 次
发布时间:2019-06-14

本文共 3307 字,大约阅读时间需要 11 分钟。

来源:

 

 
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);
}
}
}

转载于:https://www.cnblogs.com/hcbin/archive/2010/03/18/1688974.html

你可能感兴趣的文章
深入理解java集合框架(jdk1.6源码)
查看>>
php截取后台登陆密码的代码
查看>>
选假球的故事
查看>>
ul li剧中对齐
查看>>
关于 linux 的 limit 的设置
查看>>
模块搜索路径
查看>>
如何成为一名优秀的程序员?
查看>>
HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)
查看>>
C++期中考试
查看>>
Working with Characters and Strings(Chapter 2 of Windows Via C/C++)
查看>>
vim中文帮助教程
查看>>
Android 创建与解析XML(四)—— Pull方式
查看>>
CodeForces 411B 手速题
查看>>
同比和环比
查看>>
美国在抛弃慕课,中国却趋之若鹜
查看>>
SpringMvc拦截器运行原理。
查看>>
MySQL基础3
查看>>
云计算数据与信息安全防护
查看>>
全局设置导航栏
查看>>
RxJS & Angular
查看>>