Exemplo n.º 1
0
        private DataGroupInfo?GetCurrentDataGroup(ReportDataContext dataContext, Band runningBand)
        {
            if (dataContext == null)
            {
                throw new ArgumentNullException(nameof(dataContext));
            }
            if (runningBand == null)
            {
                throw new ArgumentNullException(nameof(runningBand));
            }

            var closestMember = runningBand as XtraReportBase ?? runningBand.GetParentBoundReportBand();
            var browser       = (ListBrowser)dataContext.GetDataBrowser(closestMember.DataSource, closestMember.DataMember, true);

            if (browser == null)
            {
                return(null);
            }

            var groupBand = runningBand as GroupBand;

            if (groupBand != null)
            {
                var groupRowLevel = DataGroupingUtils.GetGroupRowLevel((SortedListController)browser.ListController, groupBand.Level);
                if (groupRowLevel != null)
                {
                    return(new DataGroupInfo(
                               closestMember.DataSource,
                               closestMember.DataMember,
                               browser,
                               browser.List,
                               DataGroupingUtils.GetGroupInfo(browser, groupRowLevel.Value, browser.Position)));
                }
                runningBand = closestMember;
            }

            var detailBand = runningBand as XtraReportBase;

            if (detailBand != null)
            {
                return(new DataGroupInfo(closestMember.DataSource, closestMember.DataMember, browser, browser.List, null));
            }

            throw new InvalidOperationException($"Running band '{runningBand.Name}' cannot be used for immediate mode grouping. Calculated field {Name} must choose a {nameof(GroupBand)} or a {nameof(DetailReportBand)}.");
        }
Exemplo n.º 2
0
        private object GetImmediateModeSummary(XtraReport report)
        {
            report.AfterPrint -= report_AfterPrint;
            report.AfterPrint += report_AfterPrint;

            var dataContext = get_DataContext(report);

            if (expressionEvaluator == null)
            {
                isImmediateSummaryValid = false;
                expressionEvaluator     = new ExpressionEvaluator(new CalculatedEvaluatorContextDescriptor(report.Parameters, this, dataContext), CriteriaOperator.Parse(Expression));
            }

            var isOverridingFilter = !string.IsNullOrWhiteSpace(OverrideFilter);

            if (isOverridingFilter && overrideFilterEvaluator == null)
            {
                isImmediateSummaryValid = false;
                overrideFilterEvaluator = new ExpressionEvaluator(new CalculatedEvaluatorContextDescriptor(report.Parameters, this, dataContext), GetFilterCriteria(report, OverrideFilter));
            }


            reset(summary);

            var dg = GetCurrentDataGroup(dataContext, Running ?? report);

            if (isImmediateSummaryValid && !dg.Equals(lastDataGroupInfo))
            {
                isImmediateSummaryValid = false;
            }
            lastDataGroupInfo = dg;

            if (isImmediateSummaryValid)
            {
                return(immediateSummary);
            }

            if (dg != null)
            {
                if (dg.Value.DataSource != (DataSource ?? report.DataSource))
                {
                    throw new EndUserConfigurationException($"The running band on summary field {Name} does not have the same data source as the summary field. Otherwise, it is not able to be correlated with the summary field.");
                }
                if (!DataMemberUtils.AreEqual(DataMember, dg.Value.DataMember) && !DataMemberUtils.IsAncestor(dg.Value.DataMember, DataMember))
                {
                    throw new EndUserConfigurationException($"The running band on summary field {Name} must have the same data member or must be a parent data member. Otherwise, it is not able to be correlated with the summary field.");
                }

                var groupListBrowser = dg.Value.ListBrowser;
                var childBrowsers    = DataMemberUtils.GetChildBrowsers(dataContext, dg.Value.DataSource, dg.Value.DataMember, DataMember);

                var groupStart    = dg.Value.GroupRowInfo == null ? 0 : dg.Value.GroupRowInfo.ChildControllerRow;
                var groupRowCount = dg.Value.GroupRowInfo == null ? groupListBrowser.Count : dg.Value.GroupRowInfo.ChildControllerRowCount;

                if (isOverridingFilter)
                {
                    var originalBrowser        = childBrowsers.Length == 0 ? groupListBrowser : childBrowsers[childBrowsers.Length - 1];
                    var originalBrowserAsChild = originalBrowser as IRelatedDataBrowser;

                    var newListController = new CustomSortedListController();
                    newListController.SetList(originalBrowser.List);
                    var newBrowser = originalBrowserAsChild != null
                        ? (ListBrowser) new CustomRelatedListBrowser(originalBrowser.Parent, originalBrowserAsChild.RelatedProperty, newListController, false)
                        : new CustomListBrowser(originalBrowser.DataSource, newListController, false);
                    ((IPropertiesContainer)newBrowser).SetCustomProperties(originalBrowser.GetItemProperties().OfType <CalculatedPropertyDescriptorBase>().Cast <PropertyDescriptor>().ToArray());
                    newListController.SetBrowser(newBrowser);

                    // Filter outside the list controller so that we can ascertain grouping for rows the override filter excludes
                    newListController.Initialize(
                        ((SortedListController)originalBrowser.ListController).GetOriginalGroupFields(),
                        ((SortedListController)originalBrowser.ListController).GetSortingSummary(),
                        null);

                    if (childBrowsers.Length == 0)
                    {
                        groupListBrowser = newBrowser;
                        if (dg.Value.GroupRowInfo != null)
                        {
                            var currentListSourceIndex    = ((SortedListController)originalBrowser.ListController).GetDataController().GetListSourceRowIndex(originalBrowser.Position);
                            var currentPositionNewBrowser = newListController.GetDataController().GetControllerRow(currentListSourceIndex);
                            var newGroupInfo = DataGroupingUtils.GetGroupInfo(newBrowser, dg.Value.GroupRowInfo.Level, currentPositionNewBrowser);
                            groupStart    = newGroupInfo.ChildControllerRow;
                            groupRowCount = newGroupInfo.ChildControllerRowCount;
                        }
                        else
                        {
                            groupStart    = 0;
                            groupRowCount = newBrowser.Count;
                        }
                    }
                    else
                    {
                        childBrowsers[childBrowsers.Length - 1] = (RelatedListBrowser)newBrowser;
                    }
                }

                if (childBrowsers.Length == 0)
                {
                    for (var i = 0; i < groupRowCount; i++)
                    {
                        var currentRow = groupListBrowser.GetRow(i + groupStart);
                        if (!isOverridingFilter || ConvertOverrideFilterResult(overrideFilterEvaluator.Evaluate(currentRow)))
                        {
                            AddSummaryValue(expressionEvaluator.Evaluate(currentRow), i);
                        }
                    }
                }
                else
                {
                    var state = dataContext.SaveState();
                    try
                    {
                        groupListBrowser.Position = groupStart;
                        var sampleIndex = 0;
                        foreach (var childBrowser in childBrowsers)
                        {
                            childBrowser.Position = 0;
                        }

                        while (true)
                        {
                            var currentIndex   = childBrowsers.Length - 1;
                            var currentBrowser = childBrowsers[currentIndex];
                            if (!isOverridingFilter || ConvertOverrideFilterResult(overrideFilterEvaluator.Evaluate(currentBrowser.Current)))
                            {
                                AddSummaryValue(expressionEvaluator.Evaluate(currentBrowser.Current), sampleIndex);
                            }
                            sampleIndex++;

                            while (currentIndex > 0 && currentBrowser.Position >= currentBrowser.Count - 1)
                            {
                                currentBrowser.Position = 0;
                                currentIndex--;
                                currentBrowser = childBrowsers[currentIndex];
                            }

                            if (currentBrowser.Position >= currentBrowser.Count - 1)
                            {
                                if (groupListBrowser.Position == groupStart + groupRowCount - 1)
                                {
                                    break;
                                }
                                groupListBrowser.Position++;
                            }
                            else
                            {
                                currentBrowser.Position++;
                            }
                        }
                    }
                    finally
                    {
                        dataContext.LoadState(state);
                    }
                }
            }

            immediateSummary        = GetCurrentSummaryResult();
            isImmediateSummaryValid = true;
            return(immediateSummary);
        }