生成动态xtrareport

时间:2014-09-12 05:15:34

标签: devexpress

我想使用DevExpress生成动态XtraReport但是对于每个Employee,我想在相同pdf文件的新页面上详细介绍它。

Dim queryString As String = String.Format("SELECT * FROM Test where Status='P' order by EmployeeCode")

Dim adap As SqlDataAdapter
adap = New SqlDataAdapter(queryString, connectionString)
Dim rowsCount As Integer = -1
rowsCount = adap.Fill(ds)

Dim label As New XRLabel()
label.Width = 500
label.Font = New System.Drawing.Font("Verdana", 10.0F, FontStyle.Bold)
PageHeader1.Controls.Add(label)

If rowsCount > 0 Then
    Dim padding As Integer = 5
    Dim tableWidth As Integer = Me.PageWidth - Me.Margins.Left - Me.Margins.Right - padding * 2

    Dim dynamicTable As XRTable = XRTable.CreateTable(New Rectangle(padding, 1, tableWidth, 40), 1, 0) ' table column count

    dynamicTable.Width = tableWidth
    dynamicTable.Rows.FirstRow.Width = tableWidth
    dynamicTable.Borders = DevExpress.XtraPrinting.BorderSide.None

    dynamicTable.BorderWidth = 0
    Dim i As Integer = 0
    dynamicTable.BeginInit()
    For Each dc As DataColumn In ds.Tables(0).Columns

        Dim cell As New XRTableCell()

        Dim binding As New XRBinding("Text", ds, ds.Tables(0).Columns(i).ColumnName)
        cell.DataBindings.Add(binding)
        cell.CanGrow = False
        cell.CanShrink = True
        cell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter
        'cell.WidthF = 10
        'cell.Width = 20
        cell.Text = dc.ColumnName
        dynamicTable.Rows.FirstRow.Cells.Add(cell)
        i += 1
    Next dc

    dynamicTable.Font = New System.Drawing.Font("Verdana", 8.0F)
    dynamicTable.AdjustSize()
    dynamicTable.EndInit()
    Detail.Controls.Add(dynamicTable)
    'label.Text = String.Format("Data table: {0}", Test)

    Me.DataSource = ds
    Me.DataMember = Test

Else
    label.Text = String.Format("There's no data to display or the table doesn't exists")
End If

2 个答案:

答案 0 :(得分:0)

您的问题没有太多信息表明您想要完成的任务。我怀疑您正在尝试了解如何根据数据集设置动态数据。

检查以下代码段,其中添加了标题表(用于显示列名称)和详细信息表(用于在代码段中显示员工)。如果您在设置单元格宽度或样式等方面遇到问题,请按照以下示例进行操作,并根据需要更正报告。

来自:How to create a report dynamically in the WinForms application

Public Sub InitDetailsBasedonXRTable(ByVal rep As XtraReport)
            Dim ds As DataSet = (CType(rep.DataSource, DataSet))
            Dim colCount As Integer = ds.Tables(0).Columns.Count
            Dim colWidth As Integer = (rep.PageWidth - (rep.Margins.Left + rep.Margins.Right)) / colCount

            ' Create a table to represent headers
            Dim tableHeader As New XRTable()
            tableHeader.Height = 20
            tableHeader.Width = (rep.PageWidth - (rep.Margins.Left + rep.Margins.Right))
            Dim headerRow As New XRTableRow()
            headerRow.Width = tableHeader.Width
            tableHeader.Rows.Add(headerRow)

            tableHeader.BeginInit()

            ' Create a table to display data
            Dim tableDetail As New XRTable()
            tableDetail.Height = 20
            tableDetail.Width = (rep.PageWidth - (rep.Margins.Left + rep.Margins.Right))
            Dim detailRow As New XRTableRow()
            detailRow.Width = tableDetail.Width
            tableDetail.Rows.Add(detailRow)
            tableDetail.EvenStyleName = "EvenStyle"
            tableDetail.OddStyleName = "OddStyle"

            tableDetail.BeginInit()

            ' Create table cells, fill the header cells with text, bind the cells to data
            For i As Integer = 0 To colCount - 1
                Dim headerCell As New XRTableCell()
                headerCell.Width = colWidth
                headerCell.Text = ds.Tables(0).Columns(i).Caption

                Dim detailCell As New XRTableCell()
                detailCell.Width = colWidth
                detailCell.DataBindings.Add("Text", Nothing, ds.Tables(0).Columns(i).Caption)
                If i = 0 Then
                 headerCell.Borders = DevExpress.XtraPrinting.BorderSide.Left Or DevExpress.XtraPrinting.BorderSide.Top Or DevExpress.XtraPrinting.BorderSide.Bottom
                 detailCell.Borders = DevExpress.XtraPrinting.BorderSide.Left Or DevExpress.XtraPrinting.BorderSide.Top Or DevExpress.XtraPrinting.BorderSide.Bottom
                Else
                    headerCell.Borders = DevExpress.XtraPrinting.BorderSide.All
                    detailCell.Borders = DevExpress.XtraPrinting.BorderSide.All
                End If

                ' Place the cells into the corresponding tables
                headerRow.Cells.Add(headerCell)
                detailRow.Cells.Add(detailCell)
            Next i
            tableHeader.EndInit()
            tableDetail.EndInit()
            ' Place the table onto a report's Detail band
            rep.Bands(BandKind.PageHeader).Controls.Add(tableHeader)
            rep.Bands(BandKind.Detail).Controls.Add(tableDetail)
        End Sub 

通过以上示例,您可以设置报表布局,样式,还说明了如何根据报表页面宽度调整表格宽度。

我建议您查看How to create a report dynamically知识库文章(它说明了如何根据报告页面宽度调整表格宽度)和How to programmatically create a table and manually add rows and cells示例。

参考文献:
Create table programmatically
How to create a report dynamically

  

要限制要在页面上显示的记录数,您需要   请遵循此文档参考。它将在Win Form和   网络表单。您需要使用XRPageBreak控件来实现此目的   目标:

     

How to: Limit the Number of Records per Page

关于这个主题的更多参考:
How to limit the number of records per page of a XTraReport using XRPageBreak Control
Page Footer / How to reset page counter after each record
How to display the entire group on a single page

答案 1 :(得分:0)

这是一个迟到的答案,但也许有帮助。

理想情况下,您希望从一开始就设计报告,以便它们始终处理更多的报告(多个条目/页面)。因此,在您的情况下,设计您的报告以处理多个员工(许多页面/许多员工)。

请勿使用报表页眉和页脚,而是使用组页眉和页脚。创建分组以隔离每个分组的单个记录,例如EmployeeID。请使用Add a Group

然后将标签放在Group Header中(如果缺少则添加一个),Detail band和Group Footer。 (如果缺少则添加组页脚)

然后 - 您必须将组页脚的Page Break属性设置为After the band, except for the last entry

然后,您可以简单地将查询限制为单个页面的单个员工,或限制为每个员工的单个页面。

这意味着您在报告中拥有更多动态 - 从一开始 - 几乎不需要进行任何更改。 (即。目前几乎没有任何变化,因为你可以很容易地重新安排你的报告)