VB2005中DataGridView打印的問題。

rockyvan 2007-12-20 03:25:26
如何將DataGridView的内容直接打印出來?
打印的内容需要是“所見即所得”樣式,即DataGridView看起來什麽版式打印出來就什麽版式。
因爲DataGridView裏面有很多Cell是不同顔色的,例如紅色、藍色、黃色等。不同背景色的Cell的數據有不同含義,所以希望打印可以把DataGridView上看到的屬性都能打印出來。

請高手指點!有示例程序或代碼參考最好。
...全文
381 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
OB664284092 2012-10-16
  • 打赏
  • 举报
回复
ncqingchuan1976 2007-12-21
  • 打赏
  • 举报
回复
那需要你自己画上去。呵呵
  Dim PreView As New PrintPreviewDialog
PreView.Document = pd
Try
PreView.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.Message, "测试", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

End Try

End Sub


Private Sub MenuPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuPageSetup.Click
Dim Ps As New PageSetupDialog
Ps.Document = pd
Try
Ps.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.Message, "测试", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

End Try

End Sub
Dim pageno As Int16
Dim NewPage As Boolean
Dim RowPos As Int16

Private Sub pd_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pd.EndPrint
RowPos = 0
End Sub

Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
Dim TmpWidth As Int16, TmpLeft As Int16, Tmptop As Int16, Tolwidth As Int16
Dim ColumnLefts As New ArrayList, ColumnWidths As New ArrayList
Dim i As Int16
Dim CellHeight As Int16
Dim StrFormat As New StringFormat


Dim StringFont As Font = DataGrid.DefaultCellStyle.Font
With StrFormat
.LineAlignment = StringAlignment.Center
.Alignment = StringAlignment.Center
.Trimming = StringTrimming.EllipsisCharacter
End With

TmpLeft = e.MarginBounds.X
Tmptop = e.MarginBounds.Y

'获取打印单元格的高度和宽度
For Each Gridcol As DataGridViewColumn In DataGrid.Columns
If Not Gridcol.Visible Then Continue For
TmpWidth = Gridcol.Width
ColumnLefts.Add(TmpLeft)
ColumnWidths.Add(TmpWidth)
TmpLeft += TmpWidth
Tolwidth += TmpWidth
Next
'调整打印位置使其居中
TmpLeft = (e.MarginBounds.Width - Tolwidth) / 2

'打印表头
For Each GridCol As DataGridViewColumn In DataGrid.Columns
If Not GridCol.Visible Then Continue For

e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i) + TmpLeft, _
Tmptop, ColumnWidths(i), DataGrid.ColumnHeadersHeight))

e.Graphics.DrawString(GridCol.HeaderText, DataGrid.ColumnHeadersDefaultCellStyle.Font, _
New SolidBrush(GridCol.InheritedStyle.ForeColor), _
New RectangleF(ColumnLefts(i) + TmpLeft, Tmptop, ColumnWidths(i), _
DataGrid.ColumnHeadersHeight), StrFormat)
i += 1
Next
i = 0
StrFormat.Alignment = StringAlignment.Near '设置单元格文本的对齐方式


'打印表格和内部文本——————————

Try


'一行一行的打印
Do While RowPos <= DataGrid.Rows.Count - 1
Dim GridRow As DataGridViewRow = DataGrid.Rows(RowPos)
If GridRow.IsNewRow Then
RowPos += 1
Continue Do
End If

CellHeight = GridRow.Height




If Tmptop < e.MarginBounds.Height Then '判断是否分页

'打印表主体
For Each Cel As DataGridViewCell In GridRow.Cells
If Not Cel.OwningColumn.Visible Then Continue For

e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
New SolidBrush(Cel.InheritedStyle.ForeColor), _
New RectangleF(ColumnLefts(i) + TmpLeft, Tmptop + DataGrid.ColumnHeadersHeight, ColumnWidths(i), _
CellHeight), StrFormat) '画单元格
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i) + TmpLeft, Tmptop + DataGrid.ColumnHeadersHeight, ColumnWidths(i), CellHeight)) '画单元格内的文本
i += 1
Next
Tmptop += CellHeight
i = 0
RowPos += 1
e.HasMorePages = False

Else
e.HasMorePages = True
Exit Do
End If
Loop

Catch ex As Printing.InvalidPrinterException
MessageBox.Show(ex.Message, "测试", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try

End Sub
rockyvan 2007-12-21
  • 打赏
  • 举报
回复
會打印,但顏色不會。。呵呵。幫頂。
-------------------------------
可否介紹下你的打印方法?

另外,我的問題用DataGridView轉Excel不知道能否實現。
rockyvan 2007-12-21
  • 打赏
  • 举报
回复
ping502幫頂分5分,ncqingchuan1976辛勞分15分。
不知道我的問題能不能用DataGridView轉PDF來實現...

ping502 2007-12-21
  • 打赏
  • 举报
回复
樓上的強,夠詳細了,我就不出來獻丑了。
ping502 2007-12-20
  • 打赏
  • 举报
回复
會打印,但顏色不會。。呵呵。幫頂。

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧