/// <summary>
        /// Updates aggregate results.
        /// </summary>
        /// <param name="grid">The grid control.</param>
        /// <param name="aggregateResults">The aggregate result collection.</param>
        /// <param name="aggregateDefinitions">The aggregate definition collection.</param>
        public static void UpdateAggregates(
            GridViewDataControl grid,
            AggregateResultCollection aggregateResults,
            IList<AggregateDefinition> aggregateDefinitions)
        {
            if (grid == null)
                return;

            ProcessAggregateResults(
                grid, aggregateResults, aggregateDefinitions);

            grid.CalculateAggregates();
        }
        /// <summary>
        /// Adds new functions providing undefined value and invalidates existing functions.
        /// </summary>
        /// <param name="grid">The grid control.</param>
        /// <param name="aggregateDefinitions">The aggregate definition collection.</param>
        public static void AddOrInvalidateAggregateFunctions(GridViewDataControl grid, IList<AggregateDefinition> aggregateDefinitions)
        {
            var resultList = aggregateDefinitions.Select(
                aggregateDefinition => new AggregateResult
                {
                    ColumnName = aggregateDefinition.ColumnName, 
                    SummaryType = aggregateDefinition.SummaryType, 
                    Value = Constants.AggregateCalculatingPlaceHolder.ToString()
                }).ToList();

            var resultCollection = new AggregateResultCollection(resultList);

            UpdateAggregates(grid, resultCollection, aggregateDefinitions);
        }
        /// <summary>
        /// Takes an aggregate result and forms or updates the corresponding aggregate function.
        /// </summary>
        /// <param name="grid">The grid.</param>
        /// <param name="resultCollection">The collection of aggregate results.</param>
        /// <param name="aggregateDefinitions">The collection of aggregate definitions.</param>
        private static void ProcessAggregateResults(GridViewDataControl grid, AggregateResultCollection resultCollection, IList<AggregateDefinition> aggregateDefinitions)
        {
            if (grid == null ||
                resultCollection == null ||
                aggregateDefinitions == null)
            {
                return;
            }

            foreach (var result in resultCollection)
            {
                var aggregateDefinition = aggregateDefinitions.FirstOrDefault(
                        x => x.ColumnName == result.ColumnName && x.SummaryType == result.SummaryType);
                if (aggregateDefinition == null)
                    continue;

                var context = grid.DataContext as IHasColumns;
                if (context == null)
                    return;

                var columnItem = context.Columns.Where(x => x.Property != null).FirstOrDefault(
                        x => string.IsNullOrEmpty(x.AdditionalDataPropertyName)
                                ? x.Property.Name == result.ColumnName
                                : x.AdditionalDataPropertyName == result.ColumnName);

                if (columnItem == null)
                    return;

                var gridColumn = grid.Columns[columnItem.FullName];

                UpdateAggregationResult(gridColumn, columnItem, aggregateDefinition, result);
            }
        }