您的位置:首页 > 编程语言

精简代码实现datagridView 表格打印

2009-02-12 22:16 323 查看
'====打印 表格前 内容====
'打印文本框内容
For Each ob As Control In Me.SplitContainer1.Panel2.Controls
If TypeOf (ob) Is TextBox Then

e.Graphics.DrawString(ob.Text, ob.Font, Brushes.Black, ob.Bounds.X, ob.Bounds.Y)
End If
Next

Dim StrFormat As New StringFormat
With StrFormat
.LineAlignment = StringAlignment.Center
.Alignment = StringAlignment.Center
.Trimming = StringTrimming.None
End With
'=====打印表格标题行
Dim rect As New Rectangle
Dim strValue As String
Dim indexCol As Integer
Dim colWidth As Integer = 0
For indexCol = 0 To datagrid.Columns.Count - 1
'If datagrid.Columns(indexCol).HeaderCell.Value Is DBNull.Value Then Continue For
strValue = datagrid.Columns(indexCol).HeaderText

rect = New Rectangle(datagrid.RowHeadersWidth, 0, datagrid.Columns(indexCol).Width, datagrid.ColumnHeadersHeight)
rect.Offset(datagrid.Location + New Point(colWidth, 0))
colWidth += datagrid.Columns(indexCol).Width
e.Graphics.DrawRectangle(Pens.Black, rect)
e.Graphics.DrawString(strValue, datagrid.Font, Brushes.Black, rect, StrFormat)
Next

'====打印表格以及内容
Dim irow As Integer : Dim icol As Integer '+ New Point(0, datagrid.ColumnHeadersHeight)
strValue = ""
For irow = 0 To datagrid.Rows.Count - 1
For icol = 0 To datagrid.Columns.Count - 1
rect = datagrid.GetCellDisplayRectangle(icol, irow, False)
rect.Offset(datagrid.Location)
e.Graphics.DrawRectangle(Pens.Black, rect)
If datagrid.Rows(irow).Cells(icol).Value Is DBNull.Value Then
strValue = ""
Else
strValue = datagrid.Rows(irow).Cells(icol).FormattedValue
End If

e.Graphics.DrawString(strValue, datagrid.Font, Brushes.Black, rect, StrFormat)
Next
Next


看了其他一些代码,感觉比较繁琐,花了1整天功夫,经过不断测试,终于可以以简便的方式 实现datagridview绘制了实现后可以 按照表格实际式样以及大小进行打印了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: