Welcome to the Xceed Community | Help
Community Search  

DataGrid Filter Changed Event?

Sort Posts: Previous Next
  •  10-06-2008, 4:03 PM Post no. 15925

    DataGrid Filter Changed Event?

    I have a grid that I'm using autofilters on and am trying to detect when the user changes the filter selections.  I've dug around, but can't seem to find any event that tells me when the filters have changed.  Is there any way to detect when the filters have changed?

  •  10-07-2008, 9:01 AM Post no. 15942 in reply to 15925

    Re: DataGrid Filter Changed Event?

    Hi Eric,

      there is not such an event in the DataGridCollectionView. I will fill a feature request for this.

     A workaround would be to register to every DataGridCollectionView.AutoFilterValues CollectionChanged as follow:

     

     IDictionary<string, IList> gridFilters = dataGridCollectionView.AutoFilterValues;


    foreach( IList autofilterValues in gridFilters.Values )

    {

      INotifyCollectionChanged notifyCollectionChanged = autofilterValues as INotifyCollectionChanged;

      notifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler( AutoFilterValues_CollectionChanged );

    }

     
    Don't forget to unregister before changing the DataGridControl's ItemsSource or use the CollectionChangedEventManager to register to CollectionChanged in a WeakListener way in order to avoid memory leaks.
     
    This way, in the AutoFilterValues_CollectionChanged event handler, you will be able to retreive the "auto-filtered" field as follow:
     
    foreach( string fieldName in dataGridCollectionView.AutoFilterValues.Keys )
    {
      if( dataGridCollectionView.AutoFilterValues[ fieldName ] == targetAutoFilterValues )
        return fieldName;
    }

    return null;
     

    Christian Nadeau
    Software Developer
    Xceed Software Inc.
  •  10-07-2008, 12:56 PM Post no. 15951 in reply to 15942

    Re: DataGrid Filter Changed Event?

    I ended up hooking into the Filter event on the DataGridCollectionViewSource to do what I needed.  That event seems to fire even when the user selects filters.

            /// <summary>
            /// FiltersChanged delegate
            /// </summary>
            public delegate void FiltersChangedHandler();
            /// <summary>
            /// Raised when the grid filters are changed
            /// </summary>
            public event FiltersChangedHandler FiltersChanged;

            // Timer used to raise FiltersChanged after grid filters change
            private System.Timers.Timer FilterTimer = new System.Timers.Timer(600) { Enabled = false, AutoReset = false };

            /// <summary>
            /// Starts or restarts the filter changed timer
            /// </summary>
            private void StartFilterTimer()
            {
                if(FilterTimer.Enabled)
                    this.FilterTimer.Stop();
                this.FilterTimer.Start();
            }


            void FilterTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                FiltersChangedInvoke();
            }

            /// <summary>
            /// Raise FiltersChanged event if anything is hooked up to it
            /// </summary>
            private void FiltersChangedInvoke()
            {
                if (FiltersChanged != null)
                {
                    Dispatcher.Invoke(FiltersChanged, null);
                }
            }

            private void GridViewSource_Filter(object sender, FilterEventArgs e)
            {
                // [Re]start the timer
                this.StartFilterTimer();
            }

     

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.