如何将DataGrid中的内容打印出来,请各位高手指点,谢谢了

zhaozhongju 2001-12-13 04:27:58
各位:
我在VB6.0下连接SQLServer2000数据库,查出的结果放在DataGrid中,现在我要将DataGrid中的内容打印出来,不知该如何解决,请各位高手指点迷津,谢谢了
...全文
147 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhfh 2002-01-29
  • 打赏
  • 举报
回复
num代表打印的行
index代表打印的列
hhfh 2002-01-29
  • 打赏
  • 举报
回复
private sub form_load()
Adodc1.ConnectionString = "Provider=SQLOLEDB.1;Password=密码;Persist Security Info=True;User ID=用户名;Initial Catalog=数据库名;Data Source=服务器名"

Adodc1.RecordSource = "SELECT * FROM table"
Adodc1.Refresh
DataGrid1_Refresh
end sub


private sub command1_click()
RowHeight = 200
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
CommonDialog1.ShowPrinter

For Num = 0 To Adodc1.Recordset.RecordCount - 1
DataGrid1.Row = Num

For Index = 0 To 24
DataGrid1.Col = Index
PrintText DataGrid1.Text, LeftStart + 160 + Lblwidth + Index * 800, TopStart + 1100 + (RowHeight + 200) * (Num + 3.7)
Next
Next
Printer.EndDoc
ErrHandler:
end sub

Sub PrintFont(tName As String, nSize As Integer, bBold As Boolean, bUnderline As Boolean, bItalic As Boolean)
Printer.FontName = tName
Printer.FontSize = nSize
Printer.FontBold = bBold
Printer.FontUnderline = bUnderline
Printer.FontItalic = bItalic
End Sub

Sub PrintText(tText As String, tx As Integer, ty As Integer)
Printer.CurrentX = tx
Printer.CurrentY = ty
Printer.Print tText
End Sub

Sub DataGrid1_Refresh()

DataGrid1.Columns(0).Width = 1300
DataGrid1.Columns(1).Width = 1000
DataGrid1.Columns(0).Caption = "文件ID"
DataGrid1.Columns(1).Caption = "分类"
end sub
nixinyi 2002-01-29
  • 打赏
  • 举报
回复
关注
Richard2001 2002-01-29
  • 打赏
  • 举报
回复
下面是一个简单的预览和打印功能例子:
你在Form中间加一个PictureBox,设Appearance=0,大小比例与一张打印纸相似。
再加两个按钮:“预览“(Preview)和“打印“(Print)按钮。

你用DataGrid的内容换掉以下的打印内容,自已可以做一个预览和打印功能了:

Sub SetData(obj As Object)
If TypeOf obj Is Picture Then
obj.Cls
End If

On Error GoTo exit_data

Dim CX, CY, i, curY, curX
Dim StepRow, lColor, adjRowHeight, iBottomRight

lColor = obj.ForeColor
'首行显示公司名称
CX = 4
obj.Font.Bold = False
obj.Font.Size = 9
obj.CurrentY = CX
obj.CurrentX = obj.Width / 2 - obj.TextWidth("地球")
'obj.ForeColor = RGB(256, 0, 0)
obj.Print "地球公司"
StepRow = obj.TextHeight("A")
'标题
CX = CX + StepRow
obj.Font.Bold = True
obj.Font.Size = 2 * 9
obj.CurrentY = CX
obj.CurrentX = obj.Width / 2 - obj.TextWidth("存款")
obj.ForeColor = RGB(256, 0, 0) '红色
obj.Print "存款单据"
StepRow = obj.TextHeight("A")
'首部信息第一行
obj.ForeColor = lColor '恢复
CX = CX + StepRow
obj.Font.Bold = False
obj.Font.Size = 9
obj.CurrentY = CX: obj.CurrentX = 150
obj.Print "左"
obj.CurrentY = CX: obj.CurrentX = obj.Width / 2 - obj.TextWidth("中") / 2
obj.Print "中"
obj.CurrentY = CX: obj.CurrentX = obj.ScaleWidth - 150 - obj.TextWidth("右")
obj.Print "右"


'=================画行线=================
curY = CX + StepRow: curX = 150

For i = 1 To 5
If i = 1 Then
obj.DrawWidth = 2
Else
obj.DrawWidth = 1
End If
obj.Line (curX, curY)-(obj.ScaleWidth - 150, curY)
iBottomRight = curY
'~~~~~
adjRowHeight = obj.TextHeight("A") + 3
curY = curY + adjRowHeight + 4
Next

'=============画竖线=================
curY = CX + StepRow: curX = 150
For i = 1 To 2
obj.DrawWidth = 1
obj.Line (curX, curY)-(curX, iBottomRight)
curX = curX + obj.Width / 2 - obj.TextWidth("中") / 2
Next
obj.Line (obj.ScaleWidth - 150, curY)-(obj.ScaleWidth - 150, iBottomRight)

'==============打印数据==================
Dim str
str = "打印数据"
curY = CX + StepRow + ((adjRowHeight + 4) / 2) - obj.TextHeight("A") / 2
curX = (150 + obj.Width / 2) / 2 - obj.TextWidth(str) / 2
obj.CurrentY = curY
obj.CurrentX = curX
obj.Print str

'...

Exit Sub

exit_data:
MsgBox Error
Exit Sub
End Sub

Private Sub Preview_Click()
SetData Pic
End Sub

Private Sub Print_Click()
SetData Printer
Printer.EndDoc
End Sub
playyuer 2002-01-29
  • 打赏
  • 举报
回复
http://microinfo.top263.net/Zip/DatGrdRpt.zip

欢迎下载 DataGrid 打印解决方案 (www.playyuer.com 提供)
http://www.csdn.net/oldexpert/TopicView.asp?id=125441&table=200101
zhaozhongju 2002-01-29
  • 打赏
  • 举报
回复
怎么再没有回答的了呢???
dbcontrols 2001-12-13
  • 打赏
  • 举报
回复
把DataGrid数据源的查询语句用于数据报表.
http://www.wzjcw.net/vbgood/taishan/index.html有个打印报表的例子.

7,771

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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