小弟刚接触VB现在想 预览、打印DataGrid中的内容,
想直接用VB中的DataReport或ActiveReport ,不知道该如何着手!
可各位大哥指教,有例子最好!

解决方案 »

  1.   

    '预览按钮
    Private Sub btnPreview_Click()
    Dim rpt As New ActiveReport1    
        Set rpt.Grid = datagrid1
        rpt.Show
    End Sub'activereport设计器里的内容
    Option ExplicitPrivate m_grid As dataGrid
    Private bDone As BooleanProperty Set Grid(grd As datagrid)
    Dim col As Column
    Dim ctl As Object
    Dim iLeft As Integer    Set m_grid = grd
        
        Detail.Height = grd.RowHeight
        
        With m_grid
            For Each col In .Columns
                Set ctl = Detail.Controls.Add("DDActiveReports2.Field")
                ctl.Left = iLeft
                ctl.Top = 0
                ctl.Height = Detail.Height
                ctl.Width = col.Width
                ctl.Tag = col.ColIndex
                ctl.Font.Name = m_grid.Font.Name
                ctl.Font.Size = m_grid.Font.Size
                Fields.Add ctl.Name
                ctl.DataField = ctl.Name
                
                PageHeader.Height = m_grid.HeadLines * m_grid.RowHeight
                Set ctl = PageHeader.Controls.Add("DDActiveReports2.Label")
                ctl.Left = iLeft
                ctl.Top = 0
                ctl.Height = PageHeader.Height
                ctl.Width = col.Width
                ctl.Caption = col.Caption
                
                ctl.Font.Name = m_grid.HeadFont.Name
                ctl.Font.Size = m_grid.HeadFont.Size
                iLeft = iLeft + col.Width + 144
                If iLeft > PrintWidth Then PrintWidth = iLeft
            Next
        End With
    End PropertyPrivate Sub ActiveReport_FetchData(eof As Boolean)
    Static lRow As Long
    Dim ctl As Object
        
        If m_grid Is Nothing Then Exit Sub
        On Error GoTo ehFetchData
        
        m_grid.Row = lRow
        For Each ctl In Detail.Controls
            Fields(ctl.Name).Value = m_grid.Columns(ctl.Tag)
        Next
        lRow = lRow + 1
        eof = False
        
    exFetchData:
        Exit Sub
        
    ehFetchData:
        Err.Clear
        Resume exFetchData
    End SubPrivate Sub ActiveReport_ReportEnd()
        If m_grid Is Nothing Then Exit Sub
        m_grid.Visible = True
    End SubPrivate Sub ActiveReport_ReportStart()
        If m_grid Is Nothing Then Exit Sub
        m_grid.Visible = False
    End Sub
      

  2.   

    为什么我按上面的做了,可是会出现如下错误,
    Error 5800 : Source: Report , Event: OnError , Line number: 0  Description:Fatal Exception during script excution.Error 5000 : Error in processing report.
     [ Extended Info: CP internal=10]
      

  3.   

    你是不是用的activereport?
    这段代码我没编绎,你编绎后显示那行错了?
      

  4.   

    编译过程并没有出错,以上两个错误是在activereport  show 出来之后才弹出来的!
      

  5.   

    在ActiveReport 可以控制每個字段或標簽的內容,將對應的數據寫入
      

  6.   

    如果仅仅是打印datagrid的内容,直接使用vb的datareport就可以了.程序发布小.
    网上有许多动态生成datareport报表的基础例子,自己慢慢学一下.