Back to all posts
Crystal ReportsC#.NET

How to Change Formula Field Variables in Crystal Report Programmatically

SathishMarch 5, 2010

Sometimes you need to modify Crystal Report formula fields dynamically at runtime.

Changing Formula Fields

csharp
using CrystalDecisions.CrystalReports.Engine;

ReportDocument report = new ReportDocument();
report.Load("Report.rpt");

// Change formula field text
report.DataDefinition.FormulaFields["FormulaFieldName"].Text = "'New Value'";

// For date formula
report.DataDefinition.FormulaFields["DateFormula"].Text = 
    string.Format("Date({0},{1},{2})", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

// For numeric formula
report.DataDefinition.FormulaFields["TaxRate"].Text = "0.10";

// Apply to viewer
crystalReportViewer1.ReportSource = report;

Working with Parameters

csharp
// Set parameter values
report.SetParameterValue("CompanyName", "My Company");
report.SetParameterValue("ReportDate", DateTime.Now);

Tips

  • Formula field text must be valid Crystal Reports formula syntax
  • String values need single quotes: 'value'
  • Date values use Date(year, month, day) function
  • Always test formulas in Crystal Reports designer first
0claps
Share this post

Comments

Protected by reCAPTCHA v3

No comments yet. Be the first to comment.