Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
15
Summary on cell merge
posted
Hello.

I have a problem with the grouped cells, as seen in the image, the painted cells are the grouped ones, exactly in the TOTAL column, 3500 is correctly observed, however in the sum at the bottom it is observed that it is adding 7000, this because the cell is grouping 2 rows of that column whose amount is 3500.

The correct thing would be for it to only show me the grouped amount and for the sum to be based on that, that is, it should show me only 3500 in the sum.

I hope someone can help me, thank you.



Parents
No Data
Reply
  • 2680
    Offline posted

    Hellо,

    Thank you for posting to Infragistics Community!

    I have been looking into your question and what I can say is that this behavior is by design and the built-in summaries are implemented to account for all data items. Taking the provided screenshot as an example, even though there is only one distinct value  3500, the merged cells represent 2*3500 so the summary correctly represents the underlying data source sum for the corresponding column.

    If, however, the required behavior is really what makes sense for your use case, my suggestion is to create a custom summary that sums only the distinct values for the column.

    This can be achieved by implementing the ICustomSummaryCalculator Interface and for example collecting the distinct values in a HashSet.

     public class MergedCellsSumSummary : ICustomSummaryCalculator
     {
         HashSet<int> helperSet;
         public void AggregateCustomSummary(SummarySettings summarySettings, UltraGridRow row)
         {
             object curValue = row.GetCellValue(summarySettings.SourceColumn);
    
             if (curValue is DBNull)
             {
                 return;
             }
    
             var currentNumber = Int32.Parse(curValue.ToString());
             if (!this.helperSet.Contains(currentNumber))
             {
                 this.helperSet.Add(currentNumber);
             }
         }
    
         public void BeginCustomSummary(SummarySettings summarySettings, RowsCollection rows)
         {
             this.helperSet = new HashSet<int>();
         }
    
         public object EndCustomSummary(SummarySettings summarySettings, RowsCollection rows)
         {
             return this.helperSet.Sum();
         }
     }

     SummarySettings summaryCustom = band.Summaries.Add(
         "CustomSum",
         SummaryType.Custom,
         new MergedCellsSumSummary(),
         band.Columns["Col1"],
         SummaryPosition.UseSummaryPositionColumn,
         band.Columns["Col1"]
         );
    
     summaryCustom.DisplayFormat = "Megred cells sum = {0}";

    By the way, in my opinion, the approach with only accounting for the unique values, would only make sense in case the column is sorted, or alternatively grouped by, so that all same values are merged in a single cell. Alternatively, in case there are multiple non-adjacent “3500” cells for example, such a result might be confusing to the user. A more suitable name for the summary in this case might be “Distinct values sum”. Alternatively, a different algorithm accounting for only adjacent equal items might be adopted. This has to be implemented on application level, if going down that road.

    So, with this approach the custom sum could account only the distinct merged values, for example:

    Below you will find attached a sample demonstrating this suggestion. Please, test it on your side and let me know how it behaves.

    Best regards,
    Bozhidara Pachilova
    Associate Software Developer

    6242.UGDistinctValuesSumSummary.zip

Children
No Data