views:

834

answers:

1

I'm modifying a report at runtime in c# .Net using Crystal Reports 2008.

I'm having trouble modifying a date fields format. I have accessed the DateFieldFormat object, and modified the properties, but only the SystemDefaultType property seems to have any affect.

If the Date format is set in the Visual Report Designer I can see the details of the format in DateFieldObject, but editing these values doesn't have any affect on the report once displayed.

Here is a sample of the code i'm using:

using Statements:

using CrystalDecisions.ReportAppServer.ClientDoc;
using CrystalDecisions.ReportAppServer.Controllers;
using CrystalDecisions.ReportAppServer.DataDefModel;
using CrystalDecisions.ReportAppServer.ReportDefModel;
using CrystalDecisions.ReportAppServer.CommonObjectModel;
using CrystalDecisions.ReportAppServer.ObjectFactory;

Code:

public Form1()
{
    InitializeComponent();
    CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    ISCDReportClientDocument reportClientDocument;

    // Load Report
    reportDocument.Load(@"C:\myReport.rpt");
    reportClientDocument = reportDocument.ReportClientDocument;

    // Access Field
    FieldObject fieldObject = (FieldObject)reportClientDocument.ReportDefinition.DetailArea.Sections[0].ReportObjects["DateField1"];

    // These work, if I want to use one of the default formats
    // fieldObject.FieldFormat.DateFormat.SystemDefaultType = CrDateSystemDefaultTypeEnum.crDateSystemDefaultTypeUseLongDate;
    // fieldObject.FieldFormat.DateFormat.SystemDefaultType = CrDateSystemDefaultTypeEnum.crDateSystemDefaultTypeUseShortDate;

    // I don't want to use one of the defaults.
    fieldObject.FieldFormat.DateFormat.SystemDefaultType = CrDateSystemDefaultTypeEnum.crDateSystemDefaultTypeNotUsingDefaults;

    // I want to change the order of the values
    fieldObject.FieldFormat.DateFormat.DateOrder = CrDateOrderEnum.crDateOrderDayMonthYear;

    // Display the report in the viewer
    crystalViewer.ReportSource = rpt.rcd.ReportSource;

}

I'm assuming I'm missing something about the object model here, but I can't find any good reference to it. Can someone help me out?

Thanks!

+1  A: 

I needed the same thing and FINALLY figured out how to do it. You need to apply the modified field to the report using:

reportClientDocument.ReportDefController.ReportObjectController.Modify(fieldObject, fieldObject);
This is a good note. I think I had this but forgot to include it in the code above. I solved it but I think there was something else missing as well. -- Thanks!
theo