您的位置:首页 > 其它

推荐一款DataGridView的打印解决方案

2009-04-08 20:02 417 查看
下载演示项目[C#] - 38.5千字节
下载来源[C#] - 47.2千字节
下载演示项目[VB.NET] - 42.9千字节
下载来源[VB.NET] - 48.3千字节





介绍

有时在
DataGridView
用户需要打印具体专栏和行(或所有)。 有些案件可以是作为如下:

有在
DataGridView的
许多行,并且没有需要打印所有。
栏宽的总和比页宽也许宽,并且去除一个或更多专栏,当打印时最好的。

因此名为
PrintDGV的
类被实施了,并且可以用于所有.NET2.0应用。 我做了它DataGrid的
DataGridView的
,在C#和VB.NET。

描述

代码的主要部分是
PrintDGV
类和
PrintOptions
形式。

PrintDGV
,我们有:

举行列名的
SelectedColumns
AvailableColumns
名单。
名为
PrintDoc的PrintDocument
对象(与
BeginPrint
PrintPage
事件处理程序)
四个作用:
Print_DataGridView
: 可以叫从类之外的主函数。
PrintDoc_BeginPrint
: 初始化有些可变物开始打印。
PrintDoc_PrintPage
: 执行打印工作。
DrawFooter
: 写页数。

代码有以下特点:

支持
文本框
链接
按钮
ComboBox
复选框
图象
专栏。
当他们显示,画专栏对在页宽的适合或者画他们。
包裹细胞文本,如果电池宽度小于它的内容。
得出页步行者、日期、时间和在页的标题。

使用代码

用于代码应用,必须增加
PrintDGV
类和
PrintOptions
形式到它,作用
Print_DataGridView
然后叫。 在我们的例子中,样品项目有名为
MainForm的
一个
形式


MainForm
DataGridView
充满‘‘Persongs.mdb的’人’桌。

PrintOption
形式,
DataGridView
专栏,适合对页和标题可以由用户选择。

PrintDoc_PrintPage
事件处理程序做以下任务:

计算栏宽。
打印当前时期,由行-圈的行通过在
DataGridView的
所有行。
在‘印刷品选择了行’方式下,如果当前行没有被选择,然后跳它。
如果它到达结尾的页,则写页数并且去下页。 如果它不到达结尾的页然后:
如果它在新的页,则画标题、日期-时间、倒栽跳水和专栏(看见的检查每个专栏是否由用户选择,如果没有,跳它)。
文本框
链接
按钮
复选框
ComboBox
图象
细胞的专栏内容(看见的检查每个专栏是否由用户选择,如果没有,跳它)。
画边界。
计算每页的‘行’第一页的。
写页步行者(页数)。


Collapse
私有静态空PrintDoc_PrintPage (对象发令者,
System.Drawing.Printing.PrintPageEventArgs e)
{
int tmpWidth, i;
int tmpTop = e.MarginBounds.Top;
int tmpLeft = e.MarginBounds.Left;
int HeaderHeight=0;
尝试
{
在开始第一页之前,它的//保存

//宽度&高度倒栽跳水和CoulmnType

如果(PageNo == 1)
{
foreach (在dgv的DataGridViewColumn GridCol。专栏)
{
如果(! GridCol.Visible)继续;
//跳,如果没选择的当前专栏

如果(! PrintDGV.SelectedColumns.Contains (
GridCol.HeaderText)) 继续;
// Detemining是否专栏

//适合对页。

如果(FitToPageWidth)
tmpWidth = (int) (Math.Floor ((双) (
(双重) GridCol.Width/
(双重) TotalWidth * (双重) TotalWidth *
((双重) e.MarginBounds.Width/
(双重) TotalWidth))));
tmpWidth = GridCol.Width;
HeaderHeight =
(int) (e.Graphics.MeasureString (GridCol.HeaderText,
GridCol.InheritedStyle.Font, tmpWidth)。高度) + 11;
//保存宽度&高度headres和ColumnType

ColumnLefts.Add (tmpLeft);
ColumnWidths.Add (tmpWidth);
ColumnTypes.Add (GridCol.GetType ());
tmpLeft += tmpWidth;
}
}
//打印当前时期,行Row

当时(RowPos <= dgv。Rows.Count - 1)
{
DataGridViewRow GridRow = dgv。行[RowPos];
如果(GridRow.IsNewRow || (! PrintAllRows &&! GridRow.Selected))
{
RowPos++;
继续;
}
CellHeight = GridRow.Height;
如果(tmpTop + CellHeight >=
e.MarginBounds.Height + e.MarginBounds.Top)
{
DrawFooter (e, RowsPerPage);
NewPage =配齐;
PageNo++;
e.HasMorePages =配齐;
回归;
}
{
如果(NewPage)
{
//凹道印刷品标题

e.Graphics.DrawString (PrintTitle,
新的字体(dgv。字体, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left,
e.MarginBounds.Top-
e.Graphics.MeasureString (PrintTitle,
新的字体(dgv。字体,
FontStyle.Bold),
e.MarginBounds.Width)。高度- 13);
串起s = DateTime.Now.ToLongDateString () + ““+
DateTime.Now.ToShortTimeString ();
//凹道时间与日期

e.Graphics.DrawString (s,
新的字体(dgv。字体, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left +
(e.MarginBounds.Width -
e.Graphics.MeasureString (s,新的字体(dgv。字体,
FontStyle.Bold), e.MarginBounds.Width)。宽度),
e.MarginBounds.Top-
e.Graphics.MeasureString (PrintTitle,
新的字体(新的字体(dgv。字体,
FontStyle.Bold), FontStyle.Bold),
e.MarginBounds.Width)。高度- 13);
//凹道倒栽跳水

tmpTop = e.MarginBounds.Top;
我= 0;
foreach (在dgv的DataGridViewColumn GridCol。专栏)
{
如果(! GridCol.Visible)继续;
如果(! PrintDGV.SelectedColumns.Contains (
GridCol.HeaderText))
继续;
e.Graphics.FillRectangle (新
SolidBrush (Color.LightGray),
新的长方形((int) ColumnLefts [i], tmpTop,
(int) ColumnWidths [i], HeaderHeight));
e.Graphics.DrawRectangle (Pens.Black,
新的长方形((int) ColumnLefts [i], tmpTop,
(int) ColumnWidths [i], HeaderHeight));
e.Graphics.DrawString (GridCol.HeaderText,
GridCol.InheritedStyle.Font,
新的SolidBrush (GridCol.InheritedStyle.ForeColor),
新的RectangleF ((int) ColumnLefts [i], tmpTop,
(int) ColumnWidths [i], HeaderHeight), StrFormat);
i++;
}
NewPage =错误;
tmpTop += HeaderHeight;
}
//凹道专栏内容

我= 0;
foreach (在GridRow.Cells的DataGridViewCell Cel)
{
如果(! Cel.OwningColumn.Visible)继续;
如果(! SelectedColumns.Contains (
Cel.OwningColumn.HeaderText))
继续;
文本框专栏的//

如果(((类型) ColumnTypes [i])。命名==
“DataGridViewTextBoxColumn” ||
((类型) ColumnTypes [i])。命名==
“DataGridViewLinkColumn”)
{
e.Graphics.DrawString (Cel.Value.ToString (),
Cel.InheritedStyle.Font,
新的SolidBrush (Cel.InheritedStyle.ForeColor),
新的RectangleF ((int) ColumnLefts [i],
(浮游物) tmpTop,
(int) ColumnWidths [i],
(浮游物) CellHeight), StrFormat);
}
按钮专栏的//

如果(((类型) ColumnTypes [i])。命名==
“DataGridViewButtonColumn”)
{
CellButton.Text = Cel.Value.ToString ();
CellButton.Size =新的大小((int) ColumnWidths [i],
CellHeight);
位图bmp =新的位图(CellButton.Width,
CellButton.Height);
CellButton.DrawToBitmap (bmp,新的长方形(0, 0,
bmp. 宽度, bmp。高度));
e.Graphics.DrawImage (bmp,
新的点((int) ColumnLefts [i], tmpTop));
}
复选框专栏的//

如果(((类型) ColumnTypes [i])。命名==
“DataGridViewCheckBoxColumn”)
{
CellCheckBox.Size =新的大小(14, 14);
CellCheckBox.Checked = (bool) Cel.Value;
位图bmp =新数位映象((int) ColumnWidths [i],
CellHeight);
图表tmpGraphics = Graphics.FromImage (bmp);
tmpGraphics.FillRectangle (Brushes.White,
新的长方形(0, 0,
bmp. 宽度, bmp。高度));
CellCheckBox.DrawToBitmap (bmp,
新的长方形((int) ((bmp。宽度-
CellCheckBox.Width)/2),
(int) ((bmp。高度- CellCheckBox.Height)/2),
CellCheckBox.Width, CellCheckBox.Height));
e.Graphics.DrawImage (bmp,
新的点((int) ColumnLefts [i], tmpTop));
}
ComboBox专栏的//

如果(((类型) ColumnTypes [i])。命名==
“DataGridViewComboBoxColumn”)
{
CellComboBox.Size =新的大小((int) ColumnWidths [i],
CellHeight);
位图bmp =新的位图(CellComboBox.Width,
CellComboBox.Height);
CellComboBox.DrawToBitmap (bmp,新的长方形(0, 0,
bmp. 宽度, bmp。高度));
e.Graphics.DrawImage (bmp,
新的点((int) ColumnLefts [i], tmpTop));
e.Graphics.DrawString (Cel.Value.ToString (),
Cel.InheritedStyle.Font,
新的SolidBrush (Cel.InheritedStyle.ForeColor),
新的RectangleF ((int) ColumnLefts [i] + 1,
tmpTop, (int) ColumnWidths [i]
- 16, CellHeight), StrFormatComboBox);
}
图象专栏的//

如果(((类型) ColumnTypes [i])。命名==
“DataGridViewImageColumn”)
{
长方形CelSize =新的长方形((int) ColumnLefts [i],
tmpTop, (int) ColumnWidths [i], CellHeight);
大小ImgSize = ((图象) (Cel.FormattedValue))。大小;
e.Graphics.DrawImage ((图象) Cel.FormattedValue,
新的长方形((int) ColumnLefts [i] +
(int) ((CelSize.Width - ImgSize.Width)/2),
tmpTop + (int) ((CelSize.Height -
ImgSize.Height)/2),
((图象) (Cel.FormattedValue))。宽度,
((图象) (Cel.FormattedValue))。高度));
}
画细胞边界的//

e.Graphics.DrawRectangle (Pens.Black,
新的长方形((int) ColumnLefts [i],
tmpTop, (int) ColumnWidths [i], CellHeight));
i++;
}
tmpTop += CellHeight;
}
RowPos++;
第一页的//它计算每页的行

如果(PageNo == 1) RowsPerPage++;
}
如果(RowsPerPage == 0)回归;
//写步行者(页数)

DrawFooter (e, RowsPerPage);
e.HasMorePages =错误;
}
抓住(前的例外)
{
MessageBox.Show (前。消息, “错误”,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}

历史

被修理的二个臭虫(2007 2月22日) :

页的数量在C#代码的是错误的。
为在第一页以后的页,专栏倒栽跳水重写第一行(两个在C#代码和VB代码)。

执照

这篇文章没有明确执照附有它,而是也许在文章文本包含用法期限或下载归档自己。 如果不确定的话请通过下面讨论委员会与作者联系。

作者也许使用执照的名单可以找到这里

关于作者

Afrasiab Cheraghi

职业: 网络开发商
地点:
伊朗,伊斯兰教的共和国
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: