本人刚接触grid++report
现高分求与datagrid交互的代码
即点击打印就能把datagrid的值导入到报表中
大家帮帮忙啊
要求数据库为sql server 2000

解决方案 »

  1.   

    DATAGRID是与记录集绑定显示的,你把记录集的值赋给DATAREPROT就行了:Private Sub DataReport_Initialize()
    Set DataReport1.DataSource = AdoRec
        For Each ctl In Me.Sections.Item("Section1").Controls
        If TypeName(ctl) = "RptTextBox" Then
        Select Case ctl.Name
        Case "Text1"
        ctl.DataField = AdoRec.Fields("aa").Name
        Case "Text2"
        ctl.DataField = AdoRec.Fields("bb").Name
        Case "Text3"
        ctl.DataField = AdoRec.Fields("cc").Name
        End Select
        End If
        Next
         
        End Sub
      

  2.   

    datareport是VB自带的控件啊
    grid++report好象不行啊
      

  3.   

    grid++report自带源程序代码:form1:Dim WithEvents Report As grproLibCtl.GridppReport
    Dim cn As New ADODB.Connection
    Dim rsReports As New ADODB.RecordsetPrivate Sub Form_Load()
        Set Report = New grproLibCtl.GridppReport
        
        With cn
            .ConnectionString = GetDatabaseConnectionString()
            .Open
        End With
       
        rsReports.Open "Select * from Reports", cn, adOpenStatic, adLockOptimistic
        rsReports.MoveFirst
        rsReports.Find "Name='Customer List.grf'"
        Report.LoadFromVariant rsReports.Fields.Item("Data").Value
        
        GRDisplayViewer1.Report = Report
        GRDisplayViewer1.Start
    End SubPrivate Sub Form_Resize()
        If Me.WindowState <> vbMinimized Then GRDisplayViewer1.Move 0, GRDisplayViewer1.Top, ScaleWidth, ScaleHeight - GRDisplayViewer1.Top
    End SubPrivate Sub cmdPostColumnLayout_Click()
        GRDisplayViewer1.PostColumnLayout    Dim NewData As Variant
        NewData = Report.SaveToVariant
        
        rsReports.MoveFirst
        rsReports.Find "Name='Customer List.grf'"
        
        rsReports.Fields.Item("Data").Value = NewData
        
        rsReports.Update
    End SubPrivate Sub cmdPreview_Click(Index As Integer)
        Report.PrintPreview (True)
    End SubPrivate Sub cmdPrint_Click(Index As Integer)
        Report.PrintEx grpgsAll, True
        'Report.Print True
    End SubPrivate Sub cmdRefresh_Click()
        GRDisplayViewer1.Refresh
    End SubPrivate Sub Report_FetchRecord(pEof As Boolean)
        Dim rs As New ADODB.Recordset
        rs.Open "select * from Customers", cn
            
        GRFetchRecordFromRecordset Report, rs
        
        rs.Close
    End Sub模块GetPath:
    Option ExplicitPublic Function GetReportTemplatePath() As String
        Dim sPath As String
        sPath = App.Path
        sPath = LCase(sPath)    Dim nPos As Integer
        nPos = InStr(sPath, "samples")
        sPath = Left(sPath, nPos - 1)
        sPath = sPath + "Samples\reports\"    GetReportTemplatePath = sPath
    End FunctionPublic Function GetReportDataPath() As String
        Dim sPath As String
        sPath = App.Path
        sPath = LCase(sPath)    Dim nPos As Integer
        nPos = InStr(sPath, "samples")
        sPath = Left(sPath, nPos - 1)
        sPath = sPath + "Samples\Data"    GetReportDataPath = sPath
    End FunctionPublic Function GetReportDataPathFile() As String
        Dim sPath As String
        sPath = App.Path
        sPath = LCase(sPath)    Dim nPos As Integer
        nPos = InStr(sPath, "samples")
        sPath = Left(sPath, nPos - 1)
        sPath = sPath + "Samples\Data\NorthWind.mdb"    GetReportDataPathFile = sPath
    End FunctionPublic Function GetDatabaseConnectionString() As String
        GetDatabaseConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" _
        + GetReportDataPathFile() + ";"
    End Function模块FillRecord:
    Option ExplicitPrivate Type MatchFieldPair
       rsField As ADODB.Field
       grField As grproLibCtl.IGRField
    End TypePublic Sub GRFetchRecordFromRecordset(Report As GridppReport, rs As ADODB.Recordset)
        If rs.Bof And rs.EOF Then Exit Sub    Dim grRecordset As grproLibCtl.IGRRecordset
        Set grRecordset = Report.DetailGrid.Recordset    Dim FieldCount As Integer
        FieldCount = grRecordset.Fields.Count
        
        Dim rsFieldCount As Integer
        rsFieldCount = rs.Fields.Count
        
        Dim FieldPairs() As MatchFieldPair
        ReDim FieldPairs(FieldCount)
        
        Dim MatchFieldCount As Integer
        MatchFieldCount = 0
        Dim I As Integer
        For I = 1 To FieldCount
            Set FieldPairs(MatchFieldCount).grField = grRecordset.Fields.Item(I)
            'Set FieldPairs(MatchFieldCount).rsField = rs.Fields.Item(FieldPairs(MatchFieldCount).grField.Name)
            Dim J As Integer
            For J = 0 To rsFieldCount - 1
                If LCase(FieldPairs(MatchFieldCount).grField.Name) = LCase(rs.Fields.Item(J).Name) Then
                    Set FieldPairs(MatchFieldCount).rsField = rs.Fields.Item(J)
                    MatchFieldCount = MatchFieldCount + 1
                    Exit For
                End If
            Next
        Next    rs.MoveFirst
        Do Until rs.EOF
            Report.DetailGrid.Recordset.Append
            
             For I = 0 To MatchFieldCount - 1
                If Not IsNull(FieldPairs(I).rsField.Value) Then
                    Select Case FieldPairs(I).grField.FieldType
                    Case grftString
                        FieldPairs(I).grField.AsString = FieldPairs(I).rsField.Value
                    Case grftInteger
                        FieldPairs(I).grField.AsInteger = FieldPairs(I).rsField.Value
                    Case grftFloat
                        FieldPairs(I).grField.AsFloat = FieldPairs(I).rsField.Value
                    Case grftBoolean
                        FieldPairs(I).grField.AsBoolean = FieldPairs(I).rsField.Value
                    Case grftDateTime
                        FieldPairs(I).grField.AsDateTime = FieldPairs(I).rsField.Value
                    Case Else 'grftBinary
                        FieldPairs(I).grField.Value = FieldPairs(I).rsField.Value
                    End Select
                End If
            Next
            
            Report.DetailGrid.Recordset.Post
        
            rs.MoveNext
       Loop
    End Sub代码没看太懂
    只是知道连接的数据库是NorthWind
    请教各位大虾
    怎么写与sql server数据库连接的代码啊?
      

  4.   

    我也在做这个程序!
    我想问下楼主!你能否达到用户自定义报表格式的功能呢?
    其实我倒是有个很土的方法!
    就是当你给dataGRID上连接数据时!把你要的数据放在临时表里!
    而报表呢。。就跟着直接连接临时表!
    每次刷新DATAGRID时就把临时表的也刷新!
    这样的交互不知你满意吗?