把DataGrid数据源的查询语句用于数据报表.
http://www.wzjcw.net/vbgood/taishan/index.html有个打印报表的例子.

解决方案 »

  1.   

    http://microinfo.top263.net/Zip/DatGrdRpt.zip欢迎下载 DataGrid 打印解决方案 (www.playyuer.com 提供)
    http://www.csdn.net/oldexpert/TopicView.asp?id=125441&table=200101
      

  2.   

    下面是一个简单的预览和打印功能例子:
    你在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 SubPrivate Sub Preview_Click()
        SetData Pic
    End SubPrivate Sub Print_Click()
        SetData Printer
        Printer.EndDoc
    End Sub
      

  3.   

    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 subSub 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 SubSub PrintText(tText As String, tx As Integer, ty As Integer) 
    Printer.CurrentX = tx
    Printer.CurrentY = ty
    Printer.Print tText
    End SubSub DataGrid1_Refresh()DataGrid1.Columns(0).Width = 1300
    DataGrid1.Columns(1).Width = 1000
    DataGrid1.Columns(0).Caption = "文件ID"
    DataGrid1.Columns(1).Caption = "分类"
    end sub
      

  4.   

    num代表打印的行
    index代表打印的列