Combining, or concatenating, two or more data fields into a single cell or label on a report template can be done with a script in the report template or with a custom SQL statement on which the report template is based. This article provides some examples of the script method. Refer to the Before Print scripts for report templates article for information on using these scripts.
In the script examples, the name of the function should be replaced with the actual name of the Before Print function being used. For example,
private void OnBeforePrint
may need to be replaced with
private void xrTableCell20_BeforePrint
The programming language of the script must match the value of the Script Language property of the template. Script Language is found in the Behavior category of Property Grid when the root node (typically eCTReportTemplate1) of the Report Explorer tree is selected.
The Report Explorer, Property Grid, and Field List areas initially appear as tabs in a pane docked on the right side of Report Template Designer.
The language of this script is C# (C sharp). The script can be found in the Device Tag columns on the Field Wire List A4 report template included with the software. It concatenates the device tag and the connection point values.
This is an example of concatenating fields that appear in the Data Fields category on the Field List area; T1_DeviceTag and T1_CP appear there. This technique can be used with other fields and with other report templates.
private void xrLabel2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) { string tag = "", cp = ""; if (GetCurrentColumnValue("T1_DeviceTag") != DBNull.Value) { tag = (string)GetCurrentColumnValue("T1_DeviceTag"); } if ((tag==null || tag=="") && GetCurrentColumnValue("T1_DEVICETAG") != DBNull.Value) { tag = (string)GetCurrentColumnValue("T1_DEVICETAG"); } if (GetCurrentColumnValue("T1_CP") != DBNull.Value) cp = (string)GetCurrentColumnValue("T1_CP"); (sender as XRLabel).Text = tag + ":" + cp; }
The language of this script is C# (C sharp). It combines page descriptions 1 and 2, separated by a space. Note that using System.Data; must be added before the function definition.
using System.Data; private void OnBeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) { string ds = "", pg = "", pgdesc1 = "", pgdesc2 = ""; ds = (string)GetCurrentColumnValue("DrawingSet"); pg = (string)GetCurrentColumnValue("Page"); DataSet dataSet1 = eCTReportTemplate1.DataSource as DataSet; DataRow[] dr = dataSet1.Tables["Page Fields"].Select("DrawingSet='"+ds+"' and Page='"+pg+"'"); pgdesc1 = dr[0]["Description 001"].ToString(); pgdesc2 = dr[0]["Description 002"].ToString(); (sender as XRLabel).Text = pgdesc1 + " " + pgdesc2; }
The language of this script is VB (Visual Basic). It adds the installation and location of the wire to the tag.
Private Sub xrLabel1_BeforePrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) dim wno, ins, loc as string if me.GetCurrentColumnValue("WireNo") is DBNull.Value then wno = "" else wno = me.GetCurrentColumnValue("WireNo") end if if me.GetCurrentColumnValue("Installation") is DBNull.Value then ins = "" else ins = me.GetCurrentColumnValue("Installation") end if if me.GetCurrentColumnValue("Location") is DBNull.Value then loc = "" else loc = me.GetCurrentColumnValue("Location") end if If wno = "" Then CType(Sender, XRLabel).text = "No Label" Else CType(Sender, XRLabel).text = ins + "-" + loc + "-" + wno End If End Sub
This example concatenates the Detailed_Description fields on a Quantified BOM report template. It adds a comma and a new line between each. Because the process of combining the fields was repetitive, the concatenation is done by a separate function that is called repeatedly.
The BeforePrint script that is bound to the cell in the report template calls the concatenatedescr function for each field to be concatenated. The concatenatedescr function only adds commas and new lines when there are additional description values, so there is no wasted space when the report is run.
For this example to work, the cell that is bound to the BeforePrint script must have the Multiline property set to Yes. This property can be found in the Behavior category of the Property Grid for the cell (right-click the cell and select Properties).
string concatenatedescr (string field, string c) { string temp = "", retconcat = ""; if (GetCurrentColumnValue(field) != DBNull.Value) { temp = (string)GetCurrentColumnValue(field); } if (temp != "" && c != "") // if both the field and the concatenated string are not empty, // set the string to be returned to the concateneated string plus a comma, new line, and the field { retconcat = c + "," + Environment.NewLine + temp; } else if (temp != "" && c == "") // if the field is not empty but the concatenated string is, // set he string to be returned to the field value { retconcat = temp; } else if (temp == "") // if the field is empty, add nothing to the string to be returned // just set it to the concatenated string value { retconcat = c; } return retconcat; } private void xrTableCell12_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) { string concatenated = ""; concatenated = concatenatedescr ("Detailed_Description1",concatenated); concatenated = concatenatedescr ("Detailed_Description2",concatenated); concatenated = concatenatedescr ("Detailed_Description3",concatenated); concatenated = concatenatedescr ("Detailed_Description4",concatenated); concatenated = concatenatedescr ("Detailed_Description5",concatenated); (sender as XRLabel).Text = concatenated; }