This is a migrated thread and some comments may be shown as answers.

Custom FilteringControl

1 Answer 185 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Brian Pratt
Top achievements
Rank 1
Brian Pratt asked on 06 Apr 2010, 09:38 PM
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?


Thanks,
Brian

1 Answer, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 07 Apr 2010, 09:04 AM
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();
 
    try
    {
        if (filter)
        {
            queryable = this.FilterDistinctValues(queryable, column);
        }
 
        if (maximumValueCount.HasValue)
        {
            result = queryable.SelectDistinct(column.DataType
                , dataMemberName).Ordered().Take(maximumValueCount.Value).ToIList();
        }
        else
        {
            result = queryable.SelectDistinct(column.DataType
                , dataMemberName).Ordered().ToIList();
        }
    }
    catch
    {
        if (maximumValueCount.HasValue)
        {
            result = queryable.ToIList().AsQueryable().SelectDistinct(column.DataType
                , dataMemberName).Ordered().Take(maximumValueCount.Value);
        }
        else
        {
            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,
Ross
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.
Tags
GridView
Asked by
Brian Pratt
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Share this question
or