SAP Knowledge Base Article - Public

2448663 - Need ability to change Summary field for a Group

Symptom

In our attempts to upgrade Crystal Reports to version 2013, we need an ability to alter the GroupBy clauses for summarized fields via an API. Our attempts to do the same using existing API methods is ending up relocating the field on the report canvas.

Groups where based on SQLExpressions and now require to convert ot Commands so the SQL can be modified. One result of this update is the Summary field that used the SQLExpression as the source now needs to be redirected to the Command.Field name.

Environment

Crystal Reports for Visual Studio

Cause

The Summary Field part of the Modify method is read only, meaning you cannot alter the data source of the summary field.

Resolution

As a work around you an use the ADD() method to create a new Group with a new Summary field, sample to add a group is below:

NOTE: btnReportObjects is a text box for displaying info

DataDefController dataDefController = rptClientDoc.DataDefController;
CrystalDecisions.ReportAppServer.DataDefModel.Group group = dataDefController.DataDefinition.Groups[0];

CrystalDecisions.ReportAppServer.DataDefModel.Fields resultFields = dataDefController.DataDefinition.ResultFields;
CrystalDecisions.ReportAppServer.DataDefModel.Field fieldToSummarize = (CrystalDecisions.ReportAppServer.DataDefModel.Field)resultFields.FindField("{Employee_Options.EMPLOYEE Full Name}", CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameFormula, CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleEnglishUS);

SummaryFieldController summaryFieldController = dataDefController.SummaryFieldController;

if (summaryFieldController.CanSummarizeOn(fieldToSummarize))
{
    SummaryField summaryField = new SummaryFieldClass();
    summaryField.Group = group;
    summaryField.SummarizedField = fieldToSummarize;
    summaryField.Operation = CrSummaryOperationEnum.crSummaryOperationDistinctCount;
    summaryFieldController.Add(-1, summaryField);
}

//Above code will automatically add the summary field object to group footer section.
//Without a summaryField in GroupHeader/GroupFooter section, in CRW designer, “Group Sort Expert” item will be grayed out in menu.
//If you want to change group sort to Top N, please just add following code:

CrystalDecisions.ReportAppServer.DataDefModel.ISCRField NewSummaryField = dataDefController.DataDefinition.SummaryFields[0];

SortController sortController = dataDefController.SortController;

TopNSort topNSort = new TopNSortClass();
topNSort.SortField = NewSummaryField;
topNSort.Direction = CrSortDirectionEnum.crSortDirectionNoSortOrder;

topNSort.DiscardOthers = false;
topNSort.NIndividualGroups = 5;
topNSort.NotInTopBottomName = "Other";

sortController.Add(-1, topNSort);


    add a summary based on this field Summaryfields has no controller so only a get to add to Group change after adding to field summary.
DistinctCount (Employee_Options.EMPLOYEE Full Name, Employee_Options.EMPLOYEE Hire Date, "Monthly")

m_boFieldIndex = rptClientDoc.DataDefController.DataDefinition.SummaryFields.Find("Sum ({frhsrg.pr_bedr}, {frhsrg.artcode})",
    //m_boFieldIndex = m_boReportClientDocument.DataDefController.DataDefinition.SummaryFields.Find("Sum ({@F_Quantity}, {frhsrg.artcode})",
    CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameFormula,
                        CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault);


m_boField = (Field)rptClientDoc.DataDefController.DataDefinition.SummaryFields[m_boFieldIndex];

rptClientDoc.DataDefController.DataDefinition.SummaryFields[0].FormulaForm.ToString() = @"DistinctCount (Employee_Options.EMPLOYEE Full Name, Employee_Options.EMPLOYEE Hire Date, 'Monthly')";

#region GroupNote
CrystalDecisions.CrystalReports.Engine.Areas areas = rpt.ReportDefinition.Areas;
foreach (CrystalDecisions.CrystalReports.Engine.Area ENarea in areas)
{
    if (ENarea.Kind == AreaSectionKind.GroupHeader)
    {
        CrystalDecisions.CrystalReports.Engine.GroupAreaFormat groupAreaFormat;
        groupAreaFormat = (CrystalDecisions.CrystalReports.Engine.GroupAreaFormat)ENarea.AreaFormat;
        textBox1 = "ENGINE:\nEnableRepeatGroupHeader: " + groupAreaFormat.EnableRepeatGroupHeader.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "EnableNewPageAfter: " + groupAreaFormat.EnableNewPageAfter.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "EnableHideForDrillDown: " + groupAreaFormat.EnableHideForDrillDown.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "EnableKeepGroupTogether: " + groupAreaFormat.EnableKeepGroupTogether.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "EnableKeepTogether: " + groupAreaFormat.EnableKeepTogether.ToString(); // always returns false
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "EnableNewPageBefore: " + groupAreaFormat.EnableNewPageBefore.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "EnablePrintAtBottomOfPage: " + groupAreaFormat.EnablePrintAtBottomOfPage.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "EnableRepeatGroupHeader: " + groupAreaFormat.EnableRepeatGroupHeader.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "EnableResetPageNumberAfter: " + groupAreaFormat.EnableResetPageNumberAfter.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "EnableSuppress: " + groupAreaFormat.EnableSuppress.ToString(); // always returns false
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n\n");

        CrystalDecisions.ReportAppServer.ReportDefModel.Area oldGHArea = (CrystalDecisions.ReportAppServer.ReportDefModel.Area)rptClientDoc.ReportDefController.ReportDefinition.get_GroupHeaderArea(0);
        CrystalDecisions.ReportAppServer.ReportDefModel.ISCRGroupAreaFormat grpAreaFormat = (CrystalDecisions.ReportAppServer.ReportDefModel.ISCRGroupAreaFormat)oldGHArea.Format;
        textBox1 = "RAS:\nKeepGroupTogether: " + grpAreaFormat.EnableKeepGroupTogether.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "VisibleGroupNumberPerPage: " + grpAreaFormat.VisibleGroupNumberPerPage.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n");
        textBox1 = "RepeatGroupHeader: " + grpAreaFormat.EnableRepeatGroupHeader.ToString();
        btnReportObjects.Text += textBox1;
        btnReportObjects.AppendText("\n 'End' \n");
        textBox1 = "";
    }
}

try
{
    //CrystalDecisions.ReportAppServer.ReportDefModel.Area oldGHArea = (CrystalDecisions.ReportAppServer.ReportDefModel.Area)rptClientDoc.ReportDefController.ReportDefinition.get_GroupHeaderArea(0);
    //CrystalDecisions.ReportAppServer.ReportDefModel.Area newGHArea = new CrystalDecisions.ReportAppServer.ReportDefModel.Area();
    //CrystalDecisions.ReportAppServer.ReportDefModel.GroupAreaFormat group = (CrystalDecisions.ReportAppServer.ReportDefModel.GroupAreaFormat)oldGHArea.Format.Clone(true);
    CrystalDecisions.ReportAppServer.ReportDefModel.ISCRGroupAreaFormat grpAreaFormat = (CrystalDecisions.ReportAppServer.ReportDefModel.ISCRGroupAreaFormat)area.Format;
    //grpAreaFormat.EnableNewPageAfter = true;

    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeBackgroundColor].Text != null)
        textBox1 += "\nGroup BackgroundColor Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeBackgroundColor].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableClampPageFooter].Text != null)
        textBox1 += "\nGroup ClampPageFooter Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableClampPageFooter].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableHideForDrillDown].Text != null)
        textBox1 += "\nGroup HideForDrillDown Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableHideForDrillDown].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableKeepTogether].Text != null)
        textBox1 += "\nGroup KeepTogether Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableKeepTogether].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableNewPageAfter].Text != null)
        textBox1 += "\nGroup NewPageAfter Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableNewPageAfter].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableNewPageBefore].Text != null)
        textBox1 += "\nGroup NewPageBefore Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableNewPageBefore].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnablePrintAtBottomOfPage].Text != null)
        textBox1 += "\nGroup PrintAtBottomOfPage Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnablePrintAtBottomOfPage].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableResetPageNumberAfter].Text != null)
        textBox1 += "\nGroup ResetPageNumberAfter Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableResetPageNumberAfter].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableSuppress].Text != null)
        textBox1 += "\nGroup Suppress Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableSuppress].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableSuppressIfBlank].Text != null)
        textBox1 += "\nGroup SuppressIfBlank Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableSuppressIfBlank].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableUnderlaySection].Text != null)
        textBox1 += "\nGroup UnderlaySection Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableUnderlaySection].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeGroupNumberPerPage].Text != null)
        textBox1 += "\nGroup GroupNumberPerPage Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeGroupNumberPerPage].Text.ToString()) + "\n";
    if (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeRecordNumberPerPage].Text != null)
        textBox1 += "\nGroup RecordNumberPerPage Conditional formula: \n" + (grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeRecordNumberPerPage].Text.ToString()) + "\n";
    btnReportObjects.Text += textBox1;
    btnReportObjects.AppendText(" 'End' \n\n");

    //// this sets the new page after formula
    //grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableNewPageAfter].Text = @"if {Orders.Customer ID} < 2 then false"; //.ToString();
    //grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeEnableNewPageAfter].Syntax = CrFormulaSyntaxEnum.crFormulaSyntaxCrystal;
    //rptClientDoc.ReportDefController.ReportAreaController.SetProperty(oldGHArea, CrReportAreaPropertyEnum.crReportAreaPropertyFormat, grpAreaFormat);

    //grpAreaFormat.EnableKeepGroupTogether = true;
    //grpAreaFormat.VisibleGroupNumberPerPage = 1;
    //grpAreaFormat.EnableRepeatGroupHeader = true;

    //grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeGroupNumberPerPage].Text = @"9-8";
    //grpAreaFormat.ConditionFormulas[CrSectionAreaFormatConditionFormulaTypeEnum.crSectionAreaConditionFormulaTypeGroupNumberPerPage].Syntax = CrFormulaSyntaxEnum.crFormulaSyntaxCrystal;
    //rptClientDoc.ReportDefController.ReportAreaController.SetProperty(oldGHArea, CrReportAreaPropertyEnum.crReportAreaPropertyFormat, grpAreaFormat);

}
catch (Exception ex)
{
    btnReportObjects.Text += "\n" + ex.Message + "\n";
}

Currently may be an Enhancement request on https://ideas.sap.com/ Vote it up...

Keywords

KBA , BI-DEV-NET , BI Software Development Kits (SDKs) - .NET or Other , Problem

Product

SAP Crystal Reports, developer version for Microsoft Visual Studio