Symptom
Reproducing the Issue
C#
Resolution
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportAppServer.ClientDoc;
ReportDocument m_boReportDocument;
ISCDReportClientDocument m_boReportClientDocument;
CrystalDecisions.ReportAppServer.DataDefModel.FormulaField m_boFormulaField;
CrystalDecisions.ReportAppServer.ReportDefModel.Section m_boSection;
CrystalDecisions.ReportAppServer.DataDefModel.Fields m_boFields;
CrystalDecisions.ReportAppServer.DataDefModel.Field m_boField;
CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject m_boFieldObject;
int m_boFieldIndex;
m_boReportDocument = new ReportDocument();
m_boReportDocument.Load("..\\..\\AddRunningTotalFormula.rpt");
m_boReportClientDocument = m_boReportDocument.ReportClientDocument;
//First create the formula field
m_boFormulaField = new CrystalDecisions.ReportAppServer.DataDefModel.FormulaField();
//Set the parameters (assume it's a string and Crystal Syntax)
// this is the specific RunningTotal Formula (see Crystal Reports Help file)
m_boFormulaField.Text = "WhilePrintingRecords; CurrencyVar Amount; Amount := Amount + {Customer.Last Year's Sales};";
m_boFormulaField.Name = "RunningTotal";
m_boFormulaField.Syntax = CrystalDecisions.ReportAppServer.DataDefModel.CrFormulaSyntaxEnum.crFormulaSyntaxCrystal;
m_boFormulaField.Type = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeCurrencyField;
//Now add it to the report
m_boReportClientDocument.DataDefController.FormulaFieldController.Add(m_boFormulaField);
//Now that the formula has been created, add the newly created formula to the report
//First determine which section to add the formula field to - in this case the details section
m_boSection = m_boReportClientDocument.ReportDefController.ReportDefinition.DetailArea.Sections[0];
//Get back all the formulafields in the report
m_boFields = m_boReportClientDocument.DataDefController.DataDefinition.FormulaFields;
m_boFieldIndex = m_boFields.Find({@RunningTotal},
CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameFormula,
CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault);
m_boField = (CrystalDecisions.ReportAppServer.DataDefModel.Field)m_boFields[m_boFieldIndex];
//Set the type of field this is
m_boField.Type = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeCurrencyField;
//Now create a new Field object which will be added to the report
m_boFieldObject = new CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject();
//Set the datasource of this field object to the formula form of the above Database Field Object
m_boFieldObject.DataSourceName = m_boFormulaField.FormulaForm;
m_boFieldObject.FieldValueType = m_boFormulaField.Type;
//Now set the co-ordinates of where the field will go
m_boFieldObject.Left = 5 * 1440; //1440 twips per inc
m_boFieldObject.Width = 2 * 1440;
//And finally Add it to the report
m_boReportClientDocument.ReportDefController.ReportObjectController.Add(m_boFieldObject, m_boSection, -1);
//Display in reportviewer
crystalReportViewer1.ReportSource = m_boReportDocument;
Using Crystal Reports XI R2 Service Pack 2 and higher, you can now access the report modification APIs without any additional licensing by using the inprocess-RAS engine through the Crystal Reports .NET SDK. An example of accessing the RAS SDK through the Crystal Reports .NET SDK is as follows:
Keywords
RAS, Modification, RunningTotal, In-Process, .NET, Create, Add, Remove, Change, Modify, ReportClientDocument, inproc, FormulaField , KBA , BI-DEV-NET , BI Software Development Kits (SDKs) - .NET or Other , How To