ComponentOne FlexGrid for UWP
Features / Filtering Data
In This Topic
    Filtering Data
    In This Topic

    The C1CollectionView interface also includes support for filtering data through its Filter property. The Filter property specifies a method that is called for each item in the collection. If the method returns true, then the item is included in the view. If the method returns false, then the item is filtered out of view. This type of method is a predicate method.

    The FlexGridSamples solution included with this Studio's samples includes a SearchBox control that consists of a TextBox control where the user types a value to search for and a timer. The timer provides a small delay to allow users to type the values to search for without re-applying the filter after each character.

    When the user stops typing, the timer elapses and applies the filter using this code:

    C#
    Copy Code
    bool Filter(object item)
    {
        // get search text
        var srch = _txtSearch.Text;
        // no text? show all items
        if (string.IsNullOrEmpty(srch))
        {
            return true;
        }
        // show items that contain the text in any of the specified properties
        foreach (PropertyInfo pi in _propertyInfo)
        {
            var value = pi.GetValue(item, null) as string;
            if (value != null && value.IndexOf(srch, StringComparison.OrdinalIgnoreCase) > -1)
            {
                return true;
            }
        }
        // exclude this item...
        return false;
    }
    

     Note how the code sets the value of the Filter property using a boolean function.

    This function takes an item as a parameter, gets the value of the specified properties for the object, and returns true if any of the object's properties contain the string being searched for.

    For example, if the objects were of type "Song" and the properties specified were "Title", "Album", and "Artist", then the function would return true if the string being searched for were found in the song's title, album, or artist. This is a powerful and easy-to-use search mechanism similar to the one used in Apple's iTunes application.

    As soon as the filter is applied, the grid (and any other controls bound to the IC1CollectionView object) will reflect the result of the filter by showing only the items selected by the filter. The following code is used to filter songs using Filter property of IC1CollectionView:

    C#
    Copy Code
    var view = View as C1.Xaml.IC1CollectionView;
    if (view != null)
    {
      view.Filter = Filter;
      view.Refresh();
    }
    

    Note that filtering and grouping work perfectly well together. The image below shows a very large song list with a filter applied to it:

     

    The image shown was taken when the filter was set to the word “Water”. The filter looks for matches in all fields (song, album, artist), so all “Creedence Clearwater Revival” songs are automatically included.