Sets and gets a new filter interface.

Namespace: Dapfor.Wpf.Controls
Assembly: Dapfor.Wpf (in Dapfor.Wpf.dll) Version: 4.1.0.26317 (4.1.0.26317)

Syntax

C#
public IFilter Filter { get; set; }
Visual Basic
Public Property Filter As IFilter
	Get
	Set
Visual C++
public:
property IFilter^ Filter {
	IFilter^ get ();
	void set (IFilter^ value);
}
F#
member Filter : IFilter with get, set

Property Value

Type: IFilter

Remarks

Simply put, data filtration is managing visibility of rows in the grid. Row invisibility in the grid means that the row is still in the grid, but it is invisible together with its children. It's important to say that this row can be accessed only via GridControl.Nodes / Row.Children collection properties. In the GridControl.Rows property invisible rows are absent, as it shows only visible rows. Filtration is particularly important when data is grouped. If there are no visible rows, the whole group becomes invisible (but is not removed!). If a filtered row should be made visible again, it takes certain position according to the sorting rules if any.

Wpf GridControl presents two ways of data filtration:

  • Setting boolean in the Filtered property
  • Implementing the IFilter interface and setting it with the GridControl.Filter property

The first way is the easiest to use, however, we recommend you to favor the second one that provides definite advantages.

The IFilter interface has only one property IFilter.IsFiltered. This method is invoked when data is inserted into the data grid. It is also invoked every time Row.Update and GridControl.FilterRefresh methods are called. Therefore, grid rows always meed the filtration criteria. However, when IFilter interface is not implemented, the invocation of Row.Update does not result in data filtration and newly added data is always visible in the grid until the Row.Filtered call. Besides, when filtering conditions are changed, invocation of GridControl.FilterRefresh does not make the row visible the programmer should iterate through every row in the data grid via GridControl.Nodes and Row.Children collections to verify new conditions. There is an important thing to add regarding multi-threaded applications. Invocation of IFilter.IsFiltered method occurs regularly in the GUI thread, and it should be considered during development of multi-threaded applications. Please note that Row.Update method is thread-safe and can be invoked in any thread.

An example demonstrating how to set and use the filter:
 Copy imageCopy
//Some data object
public class Product : INotifyPropertyChanged
{
    private double price;

    public double Price
    {
        get { return price; }
        set
        {
            //If the price is not the same, change it and notify about price changing
            if (price != value)
            {
                price = value;
                //The event can be raised from any thread. The grid will synchronize thread with GUI without blocking the calling thread.
                //While painting, sorting or filtering the grid can ask this object in the GUI (!) thread to return the price value.
                if(PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Price"));
                }
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public void HowToSetFilter(GridControl grid)
{
    //Set a filter that hides all rows, that contain products with price less than 10
    grid.Filter = new Filter(row =>
    {
        //There are three ways to get price: 
        //1. From the Cell through the Value property, which returns a double value: row["Price"].Value
        //2. Through the IDataAccessor and IDataField: row.DataAccessor["Price"].Value
        //3. From the data object itself: ((Product)row.DataObject).Price

        if ((double)row["Price"].Value < 10)
        {
            //Filter the row
            return true;
        }

        //The row is not filtered
        return false;
    });

    //Populate the grid
    Product product1 = new Product();
    Product product2 = new Product();

    grid.Rows.Add(product1);
    grid.Rows.Add(product2);

    //Update the product's price
    //Data objects will notify the GridControl, and it will display only the product2. 
    //The product1 will be hidden
    product1.Price = 9;
    product2.Price = 11;
}

See Also