How to write StiExpression for Count() in code

Stimulsoft Ultimate discussion
Post Reply
User avatar
tpontow
Posts: 206
Joined: Thu Sep 06, 2012 8:46 am
Location: Bonn, Germany

How to write StiExpression for Count() in code

Post by tpontow »

Hello Stimulsoft Support,

i build my Report completly in C# Code. I'm using GroupHeaderBand and GroupFooterBand too. It worked fine so far. Now i want to use the Group Count() for displaying the number of grouped Elements for each group. How can i write the StiExpression with using Count()?

I tried the following but nothing works:

Code: Select all

StiText groupHeaderBandTextComponent = new StiText
                        {
                            ...                            
                            Text = new StiExpression(@"{Count()}"),
                            ...                        };

Code: Select all

StiText groupHeaderBandTextComponent = new StiText
                        {
                            ...                            
                            Text = new StiExpression(@"{{Count()}}"),
                            ...                        };

Code: Select all

StiText groupHeaderBandTextComponent = new StiText
                        {
                            ...                            
                            Text = new StiExpression("{{Format(\"{0}\", Count())}}"),                            
...                        };
Is it possible to use Count() in StiExpression Definition in C# code?

Thanks and regards
Thorsten Pontow
Thorsten Pontow

It is easier to write an incorrect program than to understand a correct one. (Alan J. Perlis, Epigrams in programming No. 7)
Edward
Posts: 2913
Joined: Fri Jun 09, 2006 4:02 am

Re: How to write StiExpression for Count() in code

Post by Edward »

Hi Thorsten,

Please use the GetValue event of the StiText in order to calculate the required value:

Code: Select all

e.Value = ToString(Totals.Count(GroupHeaderBand1)); 
GroupHeaderCountFromCode.png
GroupHeaderCountFromCode.png (53.63 KiB) Viewed 1666 times
Thank you,
Edward
User avatar
tpontow
Posts: 206
Joined: Thu Sep 06, 2012 8:46 am
Location: Bonn, Germany

Re: How to write StiExpression for Count() in code

Post by tpontow »

Hi Edward,

thanks for your quick response and support. Your solution works for me! Great! I even can use the text generated from text-Expression and i can set my text depending on the number of group items. Here's my code. Maybe someone can use it.

Code: Select all

private void GroupHeaderTextGetValue(object sender, StiGetValueEventArgs valueEventArgs)
{
    if (sender is StiText text)
    {
        string countText = "(1 Item)";

        if (Totals.Count(text.Parent) > 1)
        {
            countText = $"({ToString(Totals.Count(text.Parent))} Items)";
        }

        valueEventArgs.Value = $"{valueEventArgs.Value} {countText}";
    }
}
Thank you
Thorsten
Thorsten Pontow

It is easier to write an incorrect program than to understand a correct one. (Alan J. Perlis, Epigrams in programming No. 7)
Edward
Posts: 2913
Joined: Fri Jun 09, 2006 4:02 am

Re: How to write StiExpression for Count() in code

Post by Edward »

Hi Thorsten,

Great! No problem. Thank you for the code snippet, someone will definitely benefit from that one!

Thank you,
Edward
Post Reply