Custom FilteringControl

2 posts, 1 answers
  1. Brian Pratt
    Brian Pratt avatar
    28 posts
    Member since:
    Apr 2010

    Posted 06 Apr 2010 Link to this post

    I want to make a Custom FilteringControl that is very similar to the existing one, but with some slight changes.

    1) Can I find the Filtering Control template anywhere that I can modify? (or code similar ?) (the existing demo does not show enough)
    2) How do I get the list of Distinct Values created by the grids DistinctValuesLoading event that your filter control uses in my custom Filter Control?

  2. Answer
    Rossen Hristov
    Rossen Hristov avatar
    2478 posts

    Posted 07 Apr 2010 Link to this post

    Hello Brian Pratt,

    In case you have the source code you can find all that you need there.

    But anyway, I have attached all files relevant to our filtering control. I have included all the view models and auxiliary files that we use internally just for your convenience. You can see how we are handling things and decide how to develop your very own custom filtering control.

    As for the distinct values, RadGridView has a public method called GetDistinctValues. You can call if for a certain column to get its values. Here is its signature:

    /// <summary>
    /// Returns distinct values for a given column.
    /// </summary>
    /// <param name="column">The column.</param>
    /// <param name="filter">A value indicating whether to filter the distinct values
    /// base on other columns' existing filters.</param>
    /// <param name="maximumValueCount">The maximum amount of distinct values to return.
    /// If you specify null for this parameter, then all distinct values will be returned.</param>
    /// <returns>
    /// An IEnumerable containing the distinct values.
    /// </returns>
    /// <remarks>
    /// The 'filter' parameter specifies whether distinct values should be filtered according
    /// to <b>other columns' active filters.</b> For example, if we have countries and players
    /// and we have filtered by CountryX, when we request all distinct players, if 'filter' is true
    /// we will get players from CountryX only. If 'filter' is false we will get <b>all</b> distinct
    /// players.
    /// </remarks>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
    public IEnumerable GetDistinctValues(IDataFieldDescriptor column
        , bool filter
        , int? maximumValueCount)
        if (maximumValueCount.GetValueOrDefault(0) < 0)
            throw new InvalidOperationException("Cannot get a negative number of distinct values.");
        if (this.Items.CollectionView == null)
            return new object[0];
        var queryable = this.Items.CollectionView.QueryableSourceCollection;
        IEnumerable result;
        var dataMemberName = column.GetDataMemberName();
            if (filter)
                queryable = this.FilterDistinctValues(queryable, column);
            if (maximumValueCount.HasValue)
                result = queryable.SelectDistinct(column.DataType
                    , dataMemberName).Ordered().Take(maximumValueCount.Value).ToIList();
                result = queryable.SelectDistinct(column.DataType
                    , dataMemberName).Ordered().ToIList();
            if (maximumValueCount.HasValue)
                result = queryable.ToIList().AsQueryable().SelectDistinct(column.DataType
                    , dataMemberName).Ordered().Take(maximumValueCount.Value);
                result = queryable.ToIList().AsQueryable().SelectDistinct(column.DataType
                    , dataMemberName).Ordered();
        return result;

    By the way, I am sure that you have read my blog post about custom filtering, but in case you have not -- it can help you a lot with getting started. Also, in my last blog I have created another custom filtering control, which might give you new ideas.

    Let me know if I can help with anything else.

    Sincerely yours,
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
Back to Top