如题目所说,
我想把查询出来的内容进行打印,内容在datagrid中,如何实现?

解决方案 »

  1.   

    set datareport1.datasource=rst(记录集)
    或者
    set datareport1.datasource=adodc1.recordset(记录集)
      

  2.   

    datareport1是一个控件吧,不是datagrid控件吧,这样就能够实现打印?
      

  3.   


       这是我某程序中打印的一部分.参考一下.(针对MSHFLEXGRID的,你可以改一下,让它适用RS即可.Sub PrintA(PS As Long, PE As Long) '打印成绩表
      Dim a As Long
      Dim B As Long
      Dim GColWidth As Long
      Dim GRowHighe As Long
      Dim LineX As Long
      Dim LineY As Long
      Dim DltP As Long
      Dim PrintR As Long
      Dim FixLab As String
      Dim TmpWidth As Long
      Dim DltH As Long
      Dim PageStar As Long
      Dim PageEnd As Long
      '------------------------------------------------
      ProwStar = 6: PageWidth = 0
      PrintGrid.Cols = PColMax - 1
      
      PrintGrid.RowHeight(-1) = RowHei
      
      PrintGrid.Font.Name = MainFrm.MainGrid.Font.Name
      PrintGrid.Font.Size = MainFrm.MainGrid.Font.Size
      GridLeft = PrintGrid.Left - 0.5 * 567: GridTop = PrintGrid.Top
      DltP = PRowMax - 1
      
      For PageStar = PS To PE
          PrintR = (PRowMax - 1) * (PageStar - 1)
          For a = 1 To PrintGrid.Rows - 1
              For B = 0 To PColMax - 2
                  If (PrintR + a + 5) < MainFrm.MainGrid.Rows Then
                     PrintGrid.TextMatrix(a, B) = MainFrm.MainGrid.TextMatrix(PrintR + a + 5, B + 1)
                  Else
                     PrintGrid.TextMatrix(a, B) = ""
                  End If
             Next B
          Next a
         '开始打印
         Printer.Font.Name = PrintGrid.Font.Name
         Printer.Font.Size = PrintGrid.Font.Size
         Printer.ScaleTop = 0: Printer.ScaleLeft = 0
         Printer.ScaleWidth = Printer.Width + 567
         Printer.ScaleHeight = Printer.Height
         GridTop = PrintGrid.Top: GridLeft = PrintGrid.Left - 0.5 * 567
         LenLab.FontName = PrintGrid.Font.Name
         LenLab.Font.Size = PrintGrid.Font.Size
         DltH = (RowHei - LenLab.Height) \ 2
         '-----------------------------------------------------------------
         PageWidth = PrintGrid.Width
         LineY = GridTop
         For a = 0 To PrintGrid.Rows - 1
             LineX = GridLeft
             '画横线
             Printer.Line (GridLeft, LineY)-(GridLeft + PageWidth, LineY)
             For B = 0 To PrintGrid.Cols - 1
                 TmpWidth = PrintGrid.ColWidth(B)
                 If a = 0 Then
                  '输出表头,并画竖线
                    FixLab = PrintGrid.TextMatrix(0, B)
                    LenLab = FixLab: DoEvents
                    Printer.Line (LineX, GridTop)-(LineX, GridTop + PageHeight)
                    
                    Printer.CurrentX = LineX + (TmpWidth - LenLab.Width) \ 2
                    Printer.CurrentY = LineY + DltH
                    Printer.Print FixLab
                 Else
                   '输出内容
                    Printer.CurrentX = LineX + 45
                    Printer.CurrentY = LineY + DltH
                    Printer.Print PrintGrid.TextMatrix(a, B)
                 End If
                 
                 LineX = LineX + TmpWidth
                 If a = 0 And B = PrintGrid.Cols - 1 Then
                    Printer.Line (LineX, GridTop)-(LineX, GridTop + PageHeight)
                 End If
             Next B
             LineY = LineY + RowHei
             '画最后一条线
             If a = PrintGrid.Rows - 1 Then
                Printer.Line (GridLeft, LineY)-(GridLeft + PageWidth, LineY)
             End If
         Next a
         Label1 = "第" & PageStar & "页"
         If GridLeft + PrintGrid.Width > PrintPic.Width - 1200 Then
            Label1.Left = PrintPic.Width - 1200
         Else
            Label1.Left = GridLeft + PrintGrid.Width - Label1.Width
         End If
         Printer.CurrentX = Label1.Left
         Printer.CurrentY = LineY + 210
         Printer.Print Label1
         DoEvents
         If PageStar < PE Then
            Printer.NewPage
         Else
            Printer.EndDoc
         End If
      Next PageStar
    End Sub
      

  4.   

    要不干脆导出到EXCEL里打印更简单...
      

  5.   

    你那个方法真的事很复杂啊,到出到EXCEL里?怎么到?可以点击打印后直接打印马?还是得需要到EXCEL中去打印?
      

  6.   

    private sub cmdbutton1_click()  
    Set DataReport1.DataSource = rs
       DataReport1.show  
    end subPrivate Sub DataReport_Initialize()
      AddDetailReport.Sections("section1").Controls("text1").DataField = 
    form1.rs(0).name
      AddDetailReport.Sections("section1").Controls("text2").DataField =
     form1.rs(1).name
    .......
    'section1为datareport中的细节栏
    'text1,text2是细节中的 RptTextbox控件
    End Sub
      

  7.   

    to Tarzen(大冠):
    Private Sub DataReport_Initialize()
      AddDetailReport.Sections("section1").Controls("text1").DataField = 
    form1.rs(0).name        ‘他说add有问题’
      AddDetailReport.Sections("section1").Controls("text2").DataField =
     form1.rs(1).name
    .......
    'section1为datareport中的细节栏
    'text1,text2是细节中的 RptTextbox控件
    End Sub
    这个函数有问题
      

  8.   

    谁能说得具体点,我没用个datareport
      

  9.   

    我写错了,AddDetailReport 是上面所指的那个DataReport1,你可以把它该过来
    rs是RecordSet,我想你应该已经产生了一个;如过没有的话也可以生成一个
    private sub cmdbutton1_click()
    dim conn as new Adodb.connection
    dim rs as new Adodb.RecordSet
    dim connstr as string
      connstr="provider=...   data source=...."
    conn.open connstr
    dim sql as string
     sql="select * from ... where..."
    set rs=conn.execute(sql)
     Set DataReport1.DataSource = rs
      DataReport1.show  end sub
    还有你必须先创建dataReport1并在Section1中放text1 and text2
      

  10.   

    Private Sub DataReport_Initialize()这不是什么函数,这是dataReport的初使化过程,
    双击datareport 就可进入
    从vb 的project-》Add Datareport向你的program中加入datareport
      

  11.   

    我已经加入了,我已经在设计器中加入了,名字就叫作datarepor1,我都没改,我觉得数据根本就过不去,别的根本就没有毛病
      

  12.   

    不要太麻烦的话就导出到excel中打印,如果要自己搞个打印格式的话,就用 MSTOP(陈建华(东莞立晨企资)) 的方法不错。