我想使用DevExpress生成动态XtraReport,但对于每个员工,我希望在相同的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发布于 2014-09-12 17:50:19
你的问题没有太多关于你到底想要实现什么的信息。我怀疑您正在尝试了解如何根据您的数据集设置动态数据。
检查下面的代码片段,它添加了header表(用于显示列名称)和details表(用于在代码片段中显示employee )。如果您在设置单元格宽度或样式等方面遇到问题,请查看下面的示例,并根据需要更正您的报表。
来自: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示例。
参考文献:
How to create a report dynamically
要限制页面上显示的记录数,您需要遵循此文档参考。它将在Win表单和Web表单中工作。您需要使用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
发布于 2015-02-04 04:52:29
这是一个迟来的答案,但可能会有帮助。
理想情况下,您希望从一开始就设计报表,以便它们始终处理多个报表(多个条目/页面)。因此,在您的情况下,设计您的报告以处理多个员工(多个页面/多个员工)。
请勿使用报表页眉和页脚,而应使用组页眉和页脚。创建一个分组来隔离每个分组的单个记录,例如您的EmployeeID。为此,请使用Add a Group。
然后将标签放在组页眉(如果缺少,则添加一个)、详细信息栏和组页脚中。(如果缺少,请添加组页脚)
然后-必须将组脚的Page Break属性设置为After the band, except for the last entry
然后,您可以简单地将查询限制到单个页面的单个员工,或多个员工-每个员工的单个页面。
这意味着您的报告从一开始就具有更多的动态性,几乎不需要任何更改。(即与您的报告相比几乎没有任何变化,因为您可以很容易地重新排列您的报告以适应这一点。)
https://stackoverflow.com/questions/25801219
复制相似问题