You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.NET中为MS Access数据表实现单条记录分页打印预览

解决VB.NET报表每页仅显示一条员工记录的问题

原代码的核心问题是循环中每次添加的是整个DataTable,导致每页都显示所有员工数据。要实现每页一条记录,需要为每个员工单独创建仅包含当前行的DataTable,再添加到报表中。

修改后的代码:

Private Sub Generate()
    report.Clear()
    Dim dt As New DataTable()
    Dim query = "Select EMPID,[NAME],MONTHLYSALARY From tblemployee"
    Using adapter As New OleDbDataAdapter(query, Con)
        adapter.Fill(dt)
    End Using

    For Each row As DataRow In dt.Rows
        ' 创建仅包含当前行的DataTable
        Dim singleRowDt As DataTable = dt.Clone() ' 复制原表结构,不复制数据
        singleRowDt.ImportRow(row) ' 导入当前行数据

        ' 添加单行表到报表,然后新建页面
        report.AddDataTable(singleRowDt)
        report.NewPage()
    Next row
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Generate()
    report.ShowPrintPreviewDialog()
End Sub

关键修改说明:

  • 使用dt.Clone()复制原DataTable的结构(列定义),但不包含任何数据
  • 通过singleRowDt.ImportRow(row)将当前循环的员工行导入到新表中
  • 每次仅将这个单行DataTable添加到报表,确保每页只显示一条记录

内容的提问来源于stack exchange,提问作者roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 06:02:40