分享
 
 
 

DataGrid控件通用打印类.

王朝c#·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Imports System.Drawing.Printing

Imports System.Drawing.Color

Imports System.Windows.Forms

Imports System.Drawing.Font

Imports System.Drawing.PointF

Imports System.Windows.Forms.DataGrid

Imports System.Drawing.Pen

Imports System.Drawing

''=======================================

'DATAGRID控件通用打印类

'中和科技-孙利臣

'于2003年05月27日17:05

'用于打印DATAGRID控件中的数据.

'=======================================

Public Class PrintDataGrid

'用户可自定义

Private PrintFont As New Font("宋体", 10) '当前要打印文本的字体及字号

Private PrintLines As Integer = 50 '当前页共要分成多少行.

Private PrintRecordNumber As Integer = 45 '当前页共要打印的记录的行数

'以下为模块内部使用

Private DataGridSource As DataGrid

Private ev As PrintPageEventArgs

Private PrintDataGrid As PrintDocument

Private PrintPriview As PrintPreviewDialog

Private PageSetup As PageSetupDialog

Private PrintScale As Double = 1 '当前要打印的数据与DATAGRID控件内数据的比例

Private DataGridColumn As DataColumn

Private DataGridRow As DataRow

Private DataGridTable As DataTable

Private Cols As Integer '当前要打印的列

Private Rows As Integer = 1 '当前要打印的行

Private ColsCount As Integer '当前DATAGRID共有多少列

Private PrintingLineNumber As Integer = 0 '当前正要打印的行号

Private PageRecordNumber As Integer '当前要所要打印的记录行数,由计算得到.

Dim X_unit As Integer '表的基本单位

Dim Y_unit As Integer

Private PrintingPageNumber As Integer = 0 '正要打印的页号

Private PageNumber As Integer '共需要打印的页数

Private PrintRecordLeave As Integer '当前还有多少页没有打印

Private PrintRecordComplete As Integer = 0 '已经打印完的记录数

Sub New(ByVal TableSource As DataGrid)

DataGridSource = TableSource

DataGridTable = New DataTable()

DataGridTable = DataGridSource.DataSource()

ColsCount = DataGridTable.Columns.Count

End Sub

'用户自定义字体及字号

Public WriteOnly Property setPrintFont() As System.Drawing.Font

Set(ByVal Value As System.Drawing.Font)

PrintFont = Value

End Set

End Property

'设置每页要打印的的记录条数

Public WriteOnly Property setPrintRecordNumber() As Integer

Set(ByVal Value As Integer)

PrintRecordNumber = Value

End Set

End Property

Public Sub Print()

Try

PrintDataGrid = New System.Drawing.Printing.PrintDocument()

AddHandler PrintDataGrid.PrintPage, AddressOf Me.PrintDataGrid_PrintPage

'PrintDataTable.Print()

'打印机设置对话框

PageSetup = New PageSetupDialog()

PageSetup.PageSettings = PrintDataGrid.DefaultPageSettings

If PageSetup.ShowDialog() = DialogResult.Cancel Then

Exit Sub

End If

'当前页是横向还是纵向打印

'计算当前页总共可以打印的行数

If PrintDataGrid.DefaultPageSettings.Landscape = False Then

PrintLines = PrintDataGrid.DefaultPageSettings.PaperSize.Height / (PrintFont.Height + 5)

Else

PrintLines = PrintDataGrid.DefaultPageSettings.PaperSize.Width / (PrintFont.Height + 5)

End If

'如果用户选择自定义纸张大小打印,则按B5纸打印,不管实际纸张大小

If PrintDataGrid.DefaultPageSettings.PaperSize.PaperName.ToString = "custom" Then

End If

'预览窗口

PrintPriview = New PrintPreviewDialog()

PrintPriview.Document = PrintDataGrid

PrintPriview.ShowDialog()

Catch ex As Exception

MessageBox.Show("error:" & ex.ToString)

Finally

End Try

End Sub

Private Sub PrintDataGrid_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)

'A4纸 A4 纸,210 x 297 毫米。

'B5纸 B5 纸,182 x 257 毫米。

Dim strPrint As String '当前要打印的文本

Dim DrawBrush As New SolidBrush(System.Drawing.Color.Blue) '当前画笔颜色

Dim X As Integer '当前要打印的文本的横坐标

Dim Y As Integer '当前要打印的文本的列坐标

Dim DrawPoint As New PointF(X, Y)

Dim row_count As Integer '当前要打印的行

PrintRecordLeave = DataGridTable.Rows.Count - PrintRecordComplete '还有多少条记录没有打印

PageNumber = PrintRecordLeave / PrintRecordNumber '共需要打印的页数

PrintingPageNumber = 0 '因为每打印一个新页都要计算还有多少页没有打印所以以打印的页数初始为0

'将当前页分成基本的单元

If PrintDataGrid.DefaultPageSettings.Landscape = True Then

X_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Height / (DataGridTable.Columns.Count + 2)

Y_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Width / PrintLines

Else

X_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Width / (DataGridTable.Columns.Count + 2)

Y_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Height / PrintLines

End If

'计算,余下的记录条数是否还可以在一页打印,不满一页时为假

If DataGridTable.Rows.Count - PrintingPageNumber * PrintRecordNumber >= PrintRecordNumber Then

PageRecordNumber = PrintRecordNumber

Else

PageRecordNumber = (DataGridTable.Rows.Count - PrintingPageNumber * PrintRecordNumber) Mod PrintRecordNumber

End If

While PrintingPageNumber <= PageNumber

'文档标题----------------打印开始

strPrint = DataGridSource.CaptionText '文档标题

DrawPoint = New PointF(X_unit, Y_unit)

ev.Graphics.DrawString(strPrint, PrintFont, DrawBrush, DrawPoint)

'文档标题----------------打印结束

'得到DATAGRID的所有列名

Dim ColumnText(DataGridTable.Columns.Count) As String

Dim Table As Integer

For Cols = 0 To DataGridTable.Columns.Count - 1

ColumnText(Cols) = DataGridTable.Columns(Cols).ToString '得到当前所有的列名

DrawPoint = New PointF(X_unit * (Cols + 1), Y_unit * 2)

ev.Graphics.DrawString(ColumnText(Cols), PrintFont, DrawBrush, DrawPoint)

Next

DrawPoint = New PointF(X_unit, Y_unit * 2)

Call DrawLine(DrawPoint, ev) '画线

'结束---------------------得到DATAGRID的所有列名

Dim PrintingLine As Integer = 0 '当前页面已经打印的记录行数

'用于确定是否换页的标记

Dim strUpData As String = "" '当前数据的前一个数据

Dim strNonce As String = "" '当前数据

While PrintingLine < PageRecordNumber

DataGridRow = DataGridTable.Rows(PrintRecordComplete) '确定要当前要打印的记录的行号

For Cols = 0 To DataGridTable.Columns.Count - 1

DrawPoint.X = X_unit * (Cols + 1)

DrawPoint.Y = Y_unit * (PrintingLine + 1 + 2)

If Cols = 0 Then '所要根据此列的数据分页

If strUpData <> "" Then 'And strNonce <> ""

If strUpData <> DataGridRow(ColumnText(0)) Then

ev.HasMorePages = True

Exit Sub

End If

End If

End If

ev.Graphics.DrawString(DataGridRow(ColumnText(Cols)), PrintFont, DrawBrush, DrawPoint)

strUpData = DataGridRow(ColumnText(0)) '当前行数据打印完成后,将打记录的第一列保存,(也可不用此语句,只为明确)

Next

DrawPoint.X = X_unit * 1

DrawPoint.Y = Y_unit * (PrintingLine + 1 + 2)

Call DrawLine(DrawPoint, ev)

PrintingLine += 1

PrintRecordComplete += 1

'打印完最后一条记录后结束打印.

'如:当前有500条记录.从0开始打印,实际打印的为第一条记录.则打印500条时实际的是第501条记录.也就是最后一条

'datagridtable.rows.count得到就是表内的实际记录条数,共有多少条记录(从1开始),当

'printrecordcomplete>=datagridtable.rows.count也就是当前已经打印到了500条,加1后将要打印第501条,越界,则结束.

If PrintRecordComplete >= DataGridTable.Rows.Count Then

ev.HasMorePages = False

Exit Sub

End If

End While

PrintingPageNumber += 1

If PrintingPageNumber > PageNumber Then

ev.HasMorePages = False

Else

ev.HasMorePages = True

Exit While

End If

End While

End Sub

'画线 只必指定当前行的打印文字的开始位置就可,x,y为当前行文字的打印位置

Private Sub DrawLine(ByVal point As PointF, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)

Dim blackPen As New Pen(System.Drawing.Color.Black, 1)

ev.Graphics.DrawLine(blackPen, point.X, point.Y + PrintFont.Height, point.X * (ColsCount + 1), point.Y + PrintFont.Height)

End Sub

End Class

``````````````````````````````````````````````````````

Private aa As PrintDataGrid.PrintDataGrid

aa = New PrintDataGrid.PrintDataGrid(DataGrid1)

aa.setPrintFont() = New System.Drawing.Font("宋体", 12) '打印文本的字体

aa.setPrintRecordNumber = 30 '每页要打印的记录行数

aa.Print()

功能:可以直接打印DATAGRID控件中的数据,但不能对数据进行格式化,功能还在完善中:)

希望大家多多指教.谢谢.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有