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

本文提供了一个名为 PrintDGV 的类,支持 .NET2.0 应用中的 DataGridView 控件打印功能,允许用户选择特定列和行进行打印,并支持文本框、链接、按钮等多种类型的单元格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载演示项目[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,我们有:

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

代码有以下特点:

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

使用代码

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

MainFormDataGridView充满‘‘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,新的长方形(00,
bmp. 宽度, bmp。高度));
e.Graphics.DrawImage (bmp,
新的点((int) ColumnLefts [i], tmpTop));
}
复选框专栏的//

如果(((类型) ColumnTypes [i])。命名==
“DataGridViewCheckBoxColumn”)
{
CellCheckBox.Size =新的大小(1414);
CellCheckBox.Checked = (bool) Cel.Value;
位图bmp =数位映象((int) ColumnWidths [i],
CellHeight);
图表tmpGraphics = Graphics.FromImage (bmp);
tmpGraphics.FillRectangle (Brushes.White,
新的长方形(00,
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,新的长方形(00,
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



职业: 网络开发商
地点: 伊朗,伊斯兰教的共和国 伊朗,伊斯兰教的共和国

转载于:https://www.cnblogs.com/hunterzou/archive/2009/04/08/1431947.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值