白开心
IT博客
::
首页
:: ::
联系
::
聚合
::
管理
::
9 随笔 :: 76 文章 :: 28 评论 :: 0 Trackbacks
<
2024年12月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
公告
独坐在路边街角,冷风吹醒,默默地伴着我的孤影。
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(12)
给我留言
查看公开留言
查看私人留言
文章分类
.Net(学习ing...)(39)
(rss)
Asp+vbScript(14)
(rss)
JavaScript(14)
(rss)
T-SQL(8)
(rss)
经典收藏(8)
(rss)
设计模式
(rss)
文章档案
2013年4月 (1)
2013年3月 (1)
2012年7月 (1)
2012年4月 (1)
2011年10月 (1)
2011年8月 (1)
2011年3月 (1)
2011年2月 (1)
2011年1月 (1)
2010年9月 (1)
2010年6月 (1)
2010年4月 (2)
2010年1月 (1)
2009年11月 (1)
2009年10月 (6)
2009年9月 (3)
2009年8月 (2)
2009年5月 (1)
2008年10月 (2)
2008年9月 (2)
2008年7月 (2)
2008年6月 (1)
2008年3月 (1)
2008年2月 (1)
2008年1月 (1)
2007年12月 (5)
2007年11月 (4)
2007年10月 (2)
2007年9月 (2)
2007年7月 (1)
2007年6月 (2)
2007年4月 (2)
2007年1月 (1)
2006年12月 (1)
2006年11月 (2)
2006年8月 (1)
2006年7月 (1)
2006年5月 (2)
2006年4月 (1)
2006年2月 (1)
2006年1月 (1)
2005年12月 (10)
相册
PhotoShop Study For HuangHuaXiang
我的相册
收藏夹
.NET(1)
(rss)
其他类别
(rss)
搜索
最新评论
1. re: JQUERY的表单异步提交[未登录]
1111111
--111
2. re: JQUERY的表单异步提交
eqedqedasd
--123
3. re: 委托和匿名函数
评论内容较长,点击标题查看
--bracelet shopping
4. re: 递归查询
您好!你的在我这里怎么执行不了啊!
--小陈
5. re: JS操作Xml的相关方法[未登录]
不能保丰到服务器上?
--11
阅读排行榜
1. 窦房折返性心动过速(928)
2. ∷∷∷∷男人感悟一百条∷∷∷∷(710)
3. 人生致命的八个经典问题(657)
4. 秋天不回来(570)
5. 圣诞(467)
评论排行榜
1. 圣诞(0)
2. 人生致命的八个经典问题(0)
3. ∷∷∷∷男人感悟一百条∷∷∷∷(0)
4. 窦房折返性心动过速(0)
5. 秋天不回来(0)
DataGridView打印预览
DataGridView 打印 ----------
#region
DataGridView 打印 ----------
private
static
StringFormat StrFormat;
//
Holds content of a TextBox Cell to write by DrawString
private
static
StringFormat StrFormatComboBox;
//
Holds content of a Boolean Cell to write by DrawImage
private
static
Button CellButton;
//
Holds the Contents of Button Cell
private
static
CheckBox CellCheckBox;
//
Holds the Contents of CheckBox Cell
private
static
ComboBox CellComboBox;
//
Holds the Contents of ComboBox Cell
private
static
int
TotalWidth;
//
Summation of Columns widths
private
static
int
RowPos;
//
Position of currently printing row
private
static
bool
NewPage;
//
Indicates if a new page reached
private
static
int
PageNo;
//
Number of pages to print
private
static
ArrayList ColumnLefts
=
new
ArrayList();
//
Left Coordinate of Columns
private
static
ArrayList ColumnWidths
=
new
ArrayList();
//
Width of Columns
private
static
ArrayList ColumnTypes
=
new
ArrayList();
//
DataType of Columns
private
static
int
CellHeight;
//
Height of DataGrid Cell
private
static
int
RowsPerPage;
//
Number of Rows per Page
private
static
System.Drawing.Printing.PrintDocument printDoc
=
new
System.Drawing.Printing.PrintDocument();
//
PrintDocumnet Object used for printing
private
static
string
PrintTitle
=
""
;
//
Header of pages
private
static
DataGridView dgvPrint;
//
Holds DataGridView Object to print its contents
private
static
List
<
string
>
SelectedColumns
=
new
List
<
string
>
();
//
The Columns Selected by user to print.
private
static
List
<
string
>
AvailableColumns
=
new
List
<
string
>
();
//
All Columns avaiable in DataGrid
private
static
bool
PrintAllRows
=
true
;
//
True = print all rows, False = print selected rows
private
static
bool
FitToPageWidth
=
true
;
//
True = Fits selected columns to page width , False = Print columns as showed
private
static
int
HeaderHeight
=
0
;
public
static
void
Print_DataGridView(DataGridView dgv)
{
PrintPreviewDialog ppvw;
try
{
dgvPrint
=
dgv;
//
Getting all Coulmns Names in the DataGridView
AvailableColumns.Clear();
foreach
(DataGridViewColumn c
in
dgv.Columns)
{
if
(
!
c.Visible)
continue
;
AvailableColumns.Add(c.HeaderText);
}
Showing the PrintOption Form
#region
Showing the PrintOption Form
//
PrintOptions dlg = new PrintOptions(AvailableColumns);
//
if (dlg.ShowDialog() != DialogResult.OK) return;
//
PrintTitle = dlg.PrintTitle;
//
PrintAllRows = dlg.PrintAllRows;
//
FitToPageWidth = dlg.FitToPageWidth;
//
SelectedColumns = dlg.GetSelectedColumns();
#endregion
No PrintOption Form
#region
No PrintOption Form
PrintTitle
=
string
.Empty;
PrintAllRows
=
true
;
FitToPageWidth
=
true
;
SelectedColumns
=
AvailableColumns;
#endregion
RowsPerPage
=
0
;
ppvw
=
new
PrintPreviewDialog();
ppvw.Document
=
printDoc;
//
Showing the Print Preview Page
printDoc.BeginPrint
+=
new
System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
printDoc.PrintPage
+=
new
System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
((Form)ppvw).WindowState
=
FormWindowState.Maximized;
if
(ppvw.ShowDialog()
!=
DialogResult.OK)
{
printDoc.BeginPrint
-=
new
System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
printDoc.PrintPage
-=
new
System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
return
;
}
//
Printing the Documnet
printDoc.Print();
printDoc.BeginPrint
-=
new
System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
printDoc.PrintPage
-=
new
System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message,
"
Error
"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
private
static
void
PrintDoc_BeginPrint(
object
sender,
System.Drawing.Printing.PrintEventArgs e)
{
try
{
//
Formatting the Content of Text Cell to print
StrFormat
=
new
StringFormat();
StrFormat.Alignment
=
StringAlignment.Near;
StrFormat.LineAlignment
=
StringAlignment.Center;
StrFormat.Trimming
=
StringTrimming.EllipsisCharacter;
//
Formatting the Content of Combo Cells to print
StrFormatComboBox
=
new
StringFormat();
StrFormatComboBox.LineAlignment
=
StringAlignment.Center;
StrFormatComboBox.FormatFlags
=
StringFormatFlags.NoWrap;
StrFormatComboBox.Trimming
=
StringTrimming.EllipsisCharacter;
ColumnLefts.Clear();
ColumnWidths.Clear();
ColumnTypes.Clear();
CellHeight
=
0
;
RowsPerPage
=
0
;
//
For various column types
CellButton
=
new
Button();
CellCheckBox
=
new
CheckBox();
CellComboBox
=
new
ComboBox();
//
Calculating Total Widths
TotalWidth
=
0
;
foreach
(DataGridViewColumn GridCol
in
dgvPrint.Columns)
{
if
(
!
GridCol.Visible)
continue
;
if
(
!
SelectedColumns.Contains(GridCol.HeaderText))
continue
;
TotalWidth
+=
GridCol.Width;
}
PageNo
=
1
;
NewPage
=
true
;
RowPos
=
0
;
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message,
"
Error
"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private
static
void
PrintDoc_PrintPage(
object
sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
int
tmpWidth, i;
int
tmpTop
=
e.MarginBounds.Top;
int
tmpLeft
=
e.MarginBounds.Left;
try
{
//
Before starting first page, it saves Width & Height of Headers and CoulmnType
if
(PageNo
==
1
)
{
foreach
(DataGridViewColumn GridCol
in
dgvPrint.Columns)
{
if
(
!
GridCol.Visible)
continue
;
//
Skip if the current column not selected
if
(
!
SelectedColumns.Contains(GridCol.HeaderText))
continue
;
//
Detemining whether the columns are fitted to page or not.
if
(FitToPageWidth)
tmpWidth
=
(
int
)(Math.Floor((
double
)((
double
)GridCol.Width
/
(
double
)TotalWidth
*
(
double
)TotalWidth
*
((
double
)e.MarginBounds.Width
/
(
double
)TotalWidth))));
else
tmpWidth
=
GridCol.Width;
HeaderHeight
=
(
int
)(e.Graphics.MeasureString(GridCol.HeaderText,
GridCol.InheritedStyle.Font, tmpWidth).Height)
+
11
;
//
Save width & height of headres and ColumnType
ColumnLefts.Add(tmpLeft);
ColumnWidths.Add(tmpWidth);
ColumnTypes.Add(GridCol.GetType());
tmpLeft
+=
tmpWidth;
}
}
//
Printing Current Page, Row by Row
while
(RowPos
<=
dgvPrint.Rows.Count
-
1
)
{
遍历行 ---
#region
遍历行 ---
DataGridViewRow GridRow
=
dgvPrint.Rows[RowPos];
if
(GridRow.IsNewRow
||
(
!
PrintAllRows
&&
!
GridRow.Selected))
{
RowPos
++
;
continue
;
}
获取列中最大的高度 ----
#region
获取列中最大的高度 ----
int
maxHeigth
=
0
;
int
width
=
0
;
int
height
=
0
;
string
text
=
string
.Empty;
i
=
0
;
foreach
(DataGridViewCell Cel
in
GridRow.Cells)
{
遍历列
#region
遍历列
if
(
!
Cel.OwningColumn.Visible)
continue
;
if
(
!
SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
continue
;
//
For the TextBox Column
if
(((Type)ColumnTypes[i]).Name
==
"
DataGridViewTextBoxColumn
"
||
((Type)ColumnTypes[i]).Name
==
"
DataGridViewLinkColumn
"
||
((Type)ColumnTypes[i]).Name
==
"
DataGridViewButtonColumn
"
||
((Type)ColumnTypes[i]).Name
==
"
DataGridViewCheckBoxColumn
"
)
{
text
=
Cel.Value
+
string
.Empty;
width
=
(
int
)ColumnWidths[i];
}
//
For the ComboBox Column
else
if
(((Type)ColumnTypes[i]).Name
==
"
DataGridViewComboBoxColumn
"
)
{
text
=
Cel.EditedFormattedValue
+
string
.Empty;
width
=
(
int
)ColumnWidths[i];
}
//
每列所需要的实际高度
height
=
(
int
)(e.Graphics.MeasureString(text,
Cel.InheritedStyle.Font, width).Height)
+
11
;
if
(height
>
maxHeigth) maxHeigth
=
height;
#endregion
i
++
;
}
#endregion
CellHeight
=
maxHeigth;
//
GridRow.Height;
if
(tmpTop
+
CellHeight
>=
e.MarginBounds.Height
+
e.MarginBounds.Top)
{
DrawFooter(e, RowsPerPage);
NewPage
=
true
;
PageNo
++
;
e.HasMorePages
=
true
;
return
;
}
else
{
if
(NewPage)
{
//
Draw Header
e.Graphics.DrawString(PrintTitle,
new
Font(dgvPrint.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top
-
e.Graphics.MeasureString(PrintTitle,
new
Font(dgvPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height
-
13
);
String s
=
DateTime.Now.ToLongDateString()
+
"
"
+
DateTime.Now.ToShortTimeString();
e.Graphics.DrawString(s,
new
Font(dgvPrint.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left
+
(e.MarginBounds.Width
-
e.Graphics.MeasureString(s,
new
Font(dgvPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top
-
e.Graphics.MeasureString(PrintTitle,
new
Font(
new
Font(dgvPrint.Font,
FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height
-
13
);
//
Draw Columns
tmpTop
=
e.MarginBounds.Top;
i
=
0
;
foreach
(DataGridViewColumn GridCol
in
dgvPrint.Columns)
{
遍历列
#region
遍历列
if
(
!
GridCol.Visible)
continue
;
if
(
!
SelectedColumns.Contains(GridCol.HeaderText))
continue
;
e.Graphics.FillRectangle(
new
SolidBrush(Color.LightGray),
new
Rectangle((
int
)ColumnLefts[i], tmpTop,
(
int
)ColumnWidths[i], HeaderHeight));
e.Graphics.DrawRectangle(Pens.Black,
new
Rectangle((
int
)ColumnLefts[i], tmpTop,
(
int
)ColumnWidths[i], HeaderHeight));
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
new
SolidBrush(GridCol.InheritedStyle.ForeColor),
new
RectangleF((
int
)ColumnLefts[i], tmpTop,
(
int
)ColumnWidths[i], HeaderHeight), StrFormat);
i
++
;
#endregion
}
NewPage
=
false
;
tmpTop
+=
HeaderHeight;
}
//
Draw Columns Contents
i
=
0
;
foreach
(DataGridViewCell Cel
in
GridRow.Cells)
{
遍历列
#region
遍历列
if
(
!
Cel.OwningColumn.Visible)
continue
;
if
(
!
SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
continue
;
//
For the TextBox Column
if
(((Type)ColumnTypes[i]).Name
==
"
DataGridViewTextBoxColumn
"
||
((Type)ColumnTypes[i]).Name
==
"
DataGridViewLinkColumn
"
)
{
e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
new
SolidBrush(Cel.InheritedStyle.ForeColor),
new
RectangleF((
int
)ColumnLefts[i], (
float
)tmpTop,
(
int
)ColumnWidths[i], (
float
)CellHeight), StrFormat);
}
//
For the Button Column
else
if
(((Type)ColumnTypes[i]).Name
==
"
DataGridViewButtonColumn
"
)
{
CellButton.Text
=
Cel.Value.ToString();
CellButton.Size
=
new
Size((
int
)ColumnWidths[i], CellHeight);
Bitmap bmp
=
new
Bitmap(CellButton.Width, CellButton.Height);
CellButton.DrawToBitmap(bmp,
new
Rectangle(
0
,
0
,
bmp.Width, bmp.Height));
e.Graphics.DrawImage(bmp,
new
Point((
int
)ColumnLefts[i], tmpTop));
}
//
For the CheckBox Column
else
if
(((Type)ColumnTypes[i]).Name
==
"
DataGridViewCheckBoxColumn
"
)
{
CellCheckBox.Size
=
new
Size(
14
,
14
);
CellCheckBox.Checked
=
(
bool
)Cel.Value;
Bitmap bmp
=
new
Bitmap((
int
)ColumnWidths[i], CellHeight);
Graphics tmpGraphics
=
Graphics.FromImage(bmp);
tmpGraphics.FillRectangle(Brushes.White,
new
Rectangle(
0
,
0
,
bmp.Width, bmp.Height));
CellCheckBox.DrawToBitmap(bmp,
new
Rectangle((
int
)((bmp.Width
-
CellCheckBox.Width)
/
2
),
(
int
)((bmp.Height
-
CellCheckBox.Height)
/
2
),
CellCheckBox.Width, CellCheckBox.Height));
e.Graphics.DrawImage(bmp,
new
Point((
int
)ColumnLefts[i], tmpTop));
}
//
For the ComboBox Column
else
if
(((Type)ColumnTypes[i]).Name
==
"
DataGridViewComboBoxColumn
"
)
{
CellComboBox.Size
=
new
Size((
int
)ColumnWidths[i], CellHeight);
Bitmap bmp
=
new
Bitmap(CellComboBox.Width, CellComboBox.Height);
CellComboBox.DrawToBitmap(bmp,
new
Rectangle(
0
,
0
,
bmp.Width, bmp.Height));
e.Graphics.DrawImage(bmp,
new
Point((
int
)ColumnLefts[i], tmpTop));
e.Graphics.DrawString(Cel.EditedFormattedValue
+
string
.Empty, Cel.InheritedStyle.Font,
new
SolidBrush(Cel.InheritedStyle.ForeColor),
new
RectangleF((
int
)ColumnLefts[i]
+
1
, tmpTop, (
int
)ColumnWidths[i]
-
16
, CellHeight), StrFormatComboBox);
}
//
For the Image Column
else
if
(((Type)ColumnTypes[i]).Name
==
"
DataGridViewImageColumn
"
)
{
Rectangle CelSize
=
new
Rectangle((
int
)ColumnLefts[i],
tmpTop, (
int
)ColumnWidths[i], CellHeight);
Size ImgSize
=
((Image)(Cel.FormattedValue)).Size;
e.Graphics.DrawImage((Image)Cel.FormattedValue,
new
Rectangle((
int
)ColumnLefts[i]
+
(
int
)((CelSize.Width
-
ImgSize.Width)
/
2
),
tmpTop
+
(
int
)((CelSize.Height
-
ImgSize.Height)
/
2
),
((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));
}
//
Drawing Cells Borders
e.Graphics.DrawRectangle(Pens.Black,
new
Rectangle((
int
)ColumnLefts[i],
tmpTop, (
int
)ColumnWidths[i], CellHeight));
i
++
;
#endregion
}
tmpTop
+=
CellHeight;
}
RowPos
++
;
//
For the first page it calculates Rows per Page
if
(PageNo
==
1
) RowsPerPage
++
;
#endregion
}
if
(RowsPerPage
==
0
)
return
;
//
Write Footer (Page Number)
DrawFooter(e, RowsPerPage);
e.HasMorePages
=
false
;
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message,
"
Error
"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private
static
void
DrawFooter(System.Drawing.Printing.PrintPageEventArgs e,
int
RowsPerPage)
{
double
cnt
=
0
;
//
Detemining rows number to print
if
(PrintAllRows)
{
if
(dgvPrint.Rows[dgvPrint.Rows.Count
-
1
].IsNewRow)
cnt
=
dgvPrint.Rows.Count
-
2
;
//
When the DataGridView doesn't allow adding rows
else
cnt
=
dgvPrint.Rows.Count
-
1
;
//
When the DataGridView allows adding rows
}
else
cnt
=
dgvPrint.SelectedRows.Count;
//
Writing the Page Number on the Bottom of Page
string
PageNum
=
"
第
"
+
PageNo.ToString()
+
"
页,共
"
+
Math.Ceiling((
double
)(cnt
/
RowsPerPage)).ToString()
+
"
页
"
;
e.Graphics.DrawString(PageNum, dgvPrint.Font, Brushes.Black,
e.MarginBounds.Left
+
(e.MarginBounds.Width
-
e.Graphics.MeasureString(PageNum, dgvPrint.Font,
e.MarginBounds.Width).Width)
/
2
, e.MarginBounds.Top
+
e.MarginBounds.Height
+
31
);
}
#endregion
posted on 2010-04-16 11:09
白开心
阅读(1667)
评论(0)
编辑
收藏
引用
所属分类:
.Net(学习ing...)
只有注册用户
登录
后才能发表评论。
Powered by:
IT博客
Copyright © 白开心