'Generate dynamic xtrareport
I want to generate dynamic XtraReport using DevExpress but for each Employee I want the details of it on new page of same pdf file.
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
Solution 1:[1]
Your question does not have much information that what are you exactly want to accomplish. I suspect that you are trying to know that how to setup dynamic data based on your Dataset.
Check the below code snippet, which adds header table(for displaying columns name) and details table(for displaying employee in your code snippet). If you are having problem setting the cells width or styles etc then go through the below example and correct your report as you want to make it up.
From: 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
By following the above example, you can set up the report layout, styles and it also illustrates how to adjust table width according to the report page width.
I suggest you to review the How to create a report dynamically KB article (it illustrates how to adjust table width according to the report page width) and How to programmatically create a table and manually add rows and cells to it example.
References:
Create table programmatically
How to create a report dynamically
To limit the number of record to display on the page, you need to follow this documentation reference. It will work in both Win Form and Web form. You need to use the XRPageBreak control to achieve this goal:
More reference on this topic:
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
Solution 2:[2]
This is a late answer but perhaps helpful.
Ideally you want to Design your reports from the beginning so that they always handle more that one report (more than one entry / page). So in your case, design your reports to handle more than one Employee (many pages / many employees).
Do NOT use the report headers and footers, but use Group Header and Footers instead. Create a Grouping to isolate a single record per grouping, like your EmployeeID for example. Use Add a Group for this.
Then you put your labels in the Group Header (add one if missing), Detail band and Group Footer only. (Add the Group Footer if missing)
Then - you must set the Page Break property for the Group Footer to After the band, except for the last entry
Then you can simply restrict your query to a single Employee for a single page, or to many Employees - for a single page for each Employee.
This means you have more dynamics in your reports - from the outset - with almost no changes required. (ie. Almost no changes than your report is at the moment, as you can re-arrange your report to suit this quite easily)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Grantly |
