Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

20.10 Add the Database Field to the Report

Add Database Field to the Report

The code to add the database field to the report creates a new field object and uses the objects created in the previous code listings to set its properties. To try and keep the method as simple as possible, the FontClass object and Section object must be created prior to calling this method. That code will be shown in the next section.

The arguments in this method are pretty self-explanatory. You pass it the table name and field name of the database field you want to put on the report. Next you give it the object coordinates and dimensions. After that are the Section and FontColor objects.

Listing 20-8. Adding the database field to the report.
[VB.NET]
Public Shared Sub AddDatabaseField(ByVal TableName As String, ByVal FieldName As String, ByVal Top As Integer, ByVal Left As Integer, ByVal Height As Integer, ByVal Width As Integer, ByVal Section As CrystalDecisions.ReportAppServer.ReportDefModel.Section, ByVal FontColor As CrystalDecisions.ReportAppServer.ReportDefModel.FontColor, ByVal rcd As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument)
'Find the data source field to place to add to the report
Dim myField As CrystalDecisions.ReportAppServer.DataDefModel.Field
myField = GetField(TableName, FieldName, rcd)
'Create the report object that will be added to the report
Dim myFieldObject As CrystalDecisions.ReportAppServer.ReportDefModel.ISCRFieldObject
myFieldObject = New CrystalDecisions.ReportAppServer.ReportDefModel.FieldObjectClass()
myFieldObject.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindField
myFieldObject.FieldValueType = myField.Type
myFieldObject.DataSource = myField.FormulaForm
myFieldObject.Left = Left
myFieldObject.Top = Top
myFieldObject.Width = Width
myFieldObject.Height = Height
myFieldObject.FontColor = FontColor
'Add the report object to the correct section
rcd.ReportDefController.ReportObjectController.Add(myFieldObject, Section, 0)
End Sub
[C#]
public static void AddDatabaseField(string TableName, string FieldName, int Top, int Left, int Height, int Width, CrystalDecisions.ReportAppServer.ReportDefModel.Section Section,
CrystalDecisions.ReportAppServer.ReportDefModel.FontColor FontColor,
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rcd)
{
//Find the data source field to place to add to the report
CrystalDecisions.ReportAppServer.DataDefModel.Field myField;
myField = GetField(TableName, FieldName, rcd);
//Create the report object that will be added to the report
CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject myFieldObject;
myFieldObject = new CrystalDecisions.ReportAppServer.ReportDefModel.FieldObjectClass();
myFieldObject.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindField;
myFieldObject.FieldValueType = myField.Type;
myFieldObject.DataSource = myField.FormulaForm;
myFieldObject.Left = Left;
myFieldObject.Top = Top;
myFieldObject.Width = Width;
myFieldObject.Height = Height;
myFieldObject.FontColor = FontColor;
//Add the report object to the correct section
rcd.ReportDefController.ReportObjectController.Add(myFieldObject, Section, 0);
}

To keep this code as understandable as possible, I left out many of the advanced formatting properties available. For example, you can set the properties EnableCanGrow, EnableSuppress, EnableKeepTogether, etc. These properties are found in the ISCRFieldObject.Format class. If you want to set these properties as well, in the previous listing you can add code similar to the following:

myFieldObject.Format.EnableCanGrow = True
myFieldObject.Format.HyperlinkText = "http://www.CrystalReportsBook.com"
myFieldObject.Format.EnableSuppress = False

As you can see, the RAS SDK exposes many properties that let you customize the formatting of a report object. To see a complete list of properties, please consult the help file.