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
920
Filtering and XamPivotGrids
posted

Hi,

In your samples Filtering, you are using IFilterViewModel to filter the data according to what the user chooses as months.

I tried it and it worked fine with my grid that has the following format which is Brands vs Dates data.

this.Rows = XmlaDataSource.GenerateInitialItems("[Brands].[Brands]");

this.Columns = XmlaDataSource.GenerateInitialItems("[Date].[Date]");

this.Measures = XmlaDataSource.GenerateInitialItems("msvalue");

However, i have another grid that has the following info:

this.Rows = XmlaDataSource.GenerateInitialItems("[Brands].[Brands]");

this.Measures = XmlaDataSource.GenerateInitialItems("msvalue,  msvolume");

with no columns and i would like to filter both measures according to what the user chooses in a drop down. but i can't use the IFilterViewModel because it is a IMeasureViewModel. and i didnt know how to go on from there. can you please help?

Below is the code used for filtering using the IFilterViewModel.

 

private void FilterDates(DateTime beginning, DateTime ending)

        {

            //Run filtering in background process in order to not hang the main UI of your app

            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += WorkerDoWork;

            worker.RunWorkerCompleted += (s, e) =>

            {

                bool allUnchecked = true;

                IFilterViewModel fvm = FindDateViewModel((DataSourceBase)this.pivotGrid.DataSource);

                foreach(IFilterMember filterMember in fvm.FilterMembers)

                {

                    if (filterMember.IsSelected != false)

                    {

                        allUnchecked = false;

                        break;

                    }

                }

 

                if (allUnchecked)

                {

                    MessageBox.Show("There is no data for the last " + (this.monthsToFilter.SelectedItem as ComboBoxItem).Content.ToString() + " months");

                }

                else

                {

                    this.pivotGrid.DataSource.RefreshGrid();

                    this.isBusyIndicator.Visibility = Visibility.Collapsed;

                }

            };

 

            this.isBusyIndicator.Visibility = Visibility.Visible;

            worker.RunWorkerAsync(new WorkerArguments()

            {

                DataSource = this.pivotGrid.DataSource as DataSourceBase,

                Beginning = beginning,

                Ending = ending

            });

        }

 

        void WorkerDoWork(object sender, DoWorkEventArgs e)

        {

            WorkerArguments args = e.Argument as WorkerArguments;

            if (args == null)

                return;

 

            //Find the Date view model or if not found create it in the Filters area

            IFilterViewModel fvm = FindDateViewModel(args.DataSource) ?? CreateViewModel(args.DataSource);

 

            if (fvm == null) return;

 

            //Manual reset event will be used to wait for the members to load

            //when they are expanded or selected

            fvm.LoadFilterMembersCompleted += (sender1, e1) => _manualResetEvent.Set();

 

            if (fvm.FilterMembers != null)

            {

                foreach (IFilterMember member in fvm.FilterMembers)

                {

                    //Recursively go through all members that need to be interacted with

                    DrillIn(member, args);

                }

            }

 

            e.Result = fvm;

        }

 

        private IFilterViewModel FindDateViewModel(DataSourceBase dataSource)

        {

 

            for (int i = 0; i < dataSource.Filters.Count; i++)

            {

                if (((IFilterViewModel)dataSource.Filters[i]).Caption == "Date")

                {

                    return dataSource.Filters[i] as IFilterViewModel;

                }

            }

            for (int i = 0; i < dataSource.Rows.Count; i++)

            {

                if (((IFilterViewModel)dataSource.Rows[i]).Caption == "Date")

                {

                    return dataSource.Rows[i] as IFilterViewModel;

                }

            }

            for (int i = 0; i < dataSource.Columns.Count; i++)

            {

                if (((IFilterViewModel)dataSource.Columns[i]).Caption == "Date")

                {

                    return dataSource.Columns[i] as IFilterViewModel;

                }

            }

 

            return null;

        }

        private IFilterViewModel CreateViewModel(DataSourceBase dataSource)

        {

            foreach (IDimension dim in dataSource.Cube.Dimensions)

            {

                foreach (IHierarchy hierarchy in dim.Hierarchies)

                {

                    if (hierarchy.UniqueName == "[Date].[Date]")

                    {

                        IFilterViewModel fvm = dataSource.CreateFilterViewModel(hierarchy);

                        this.pivotGrid.Dispatcher.BeginInvoke(

                            () => this.pivotGrid.DataSource.Filters.Add(fvm)

                        );

                        return fvm;

                    }

                }

            }

            return null;

        }

 

        private void DrillIn(IFilterMember filterMember, WorkerArguments args)

        {

            _manualResetEvent = new ManualResetEvent(false);

 

            //Expand all levels to the individual date level

            //and then compare if each date is between the start and end date.

            //If it is select it, otherwise disselect it.                                                                

            if (filterMember.Member.LevelName == "Dates")

            {

                if (

                    (DateTime.Parse(filterMember.Member.Caption).CompareTo(args.Beginning) >= 0) &&

                    (DateTime.Parse(filterMember.Member.Caption).CompareTo(args.Ending) < 0)

                )

                {

                    this.Dispatcher.BeginInvoke(() => filterMember.FilterSource.IsSelected = true);

                }

                else

                {

                    this.Dispatcher.BeginInvoke(() => filterMember.FilterSource.IsSelected = false);

                }

                return;

            }

            else

            {

                if (!filterMember.IsExpanded)

                {

                    this.Dispatcher.BeginInvoke(() => filterMember.IsExpanded = true);

                    _manualResetEvent.WaitOne();

                }

            }

 

            for (int i = 0; i < filterMember.FilterMembers.Count; i++)

            {

                DrillIn(filterMember.FilterMembers[i], args);

            }

        }