Click here to Skip to main content
15,897,187 members
Articles / Desktop Programming / WPF

Automatic WPF Toolkit DataGrid Filtering

Rate me:
Please Sign up or sign in to vote.
4.91/5 (109 votes)
21 May 2010BSD6 min read 1.4M   23.5K   167  
This article discusses a component that enables automated content filtering for the WPF Toolkit DataGrid.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataGridFilterLibrary.Support;

namespace DataGridFilterLibrary.Querying
{
    internal class StringFilterExpressionCreator
    {
        const string WildcardAnyString = "%";

        private enum StringExpressionFunction
        {
            Undefined = 0,
            StartsWith = 1,
            IndexOf = 2,
            EndsWith = 3
        }

        FilterData filterData;
        List<object> paramseters;
        ParameterCounter paramCounter;

        internal int ParametarsCrated { get { return paramseters.Count; } }

        internal StringFilterExpressionCreator(
            ParameterCounter paramCounter, FilterData filterData, List<object> paramseters)
        {
            this.paramCounter = paramCounter;
            this.filterData = filterData;
            this.paramseters = paramseters;
        }

        internal string Create()
        {
            StringBuilder filter = new StringBuilder();
            
            List<string> filterList = parse(this.filterData.QueryString);

            for (int i = 0; i < filterList.Count; i++)
            {
                if (i > 0) filter.Append(" and ");

                filter.Append(filterList[i]);
            }

            return filter.ToString();
        }

        private List<string> parse(string filterString)
        {
            string token = null;
            int i = 0;
            bool expressionCompleted = false;
            List<string> filter = new List<string>();
            string expressionValue = String.Empty;
            StringExpressionFunction function = StringExpressionFunction.Undefined;

            do
            {
                token = i < filterString.Length ? filterString[i].ToString() : null;

                if (token == WildcardAnyString || token == null)
                {
                    if (expressionValue.StartsWith(WildcardAnyString) && token != null)
                    {
                        function = StringExpressionFunction.IndexOf;

                        expressionCompleted = true;
                    }
                    else if (expressionValue.StartsWith(WildcardAnyString) && token == null)
                    {
                        function = StringExpressionFunction.EndsWith;

                        expressionCompleted = false;
                    }
                    else
                    {
                        function = StringExpressionFunction.StartsWith;

                        if (filterString.Length - 1 > i) expressionCompleted = true;
                    }
                }

                if (token == null)
                {
                    expressionCompleted = true;
                }

                expressionValue += token;

                if (expressionCompleted
                    && function != StringExpressionFunction.Undefined
                    && expressionValue != String.Empty)
                {
                    string expressionValueCopy = String.Copy(expressionValue);

                    expressionValueCopy = expressionValueCopy.Replace(WildcardAnyString, String.Empty);

                    if (expressionValueCopy != String.Empty)
                    {
                        filter.Add(createFunction(function, expressionValueCopy));
                    }

                    function = StringExpressionFunction.Undefined;

                    expressionValue = expressionValue.EndsWith(WildcardAnyString) ? WildcardAnyString : String.Empty;

                    expressionCompleted = false;
                }

                i++;

            } while (token != null);

            return filter;
        }

        private string createFunction(
            StringExpressionFunction function, string value)
        {
            StringBuilder filter = new StringBuilder();

            paramseters.Add(value);

            filter.Append(filterData.ValuePropertyBindingPath);

            if (filterData.ValuePropertyType.IsGenericType
                && filterData.ValuePropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                filter.Append(".Value");
            }

            paramCounter.Increment();
            paramCounter.Increment();

            filter.Append(".ToString()." + function.ToString() + "(@" + (paramCounter.ParameterNumber - 1) + ", @" + (paramCounter.ParameterNumber) + ")");

            if (function == StringExpressionFunction.IndexOf)
            {
                filter.Append(" != -1 ");
            }

            paramseters.Add(StringComparison.CurrentCultureIgnoreCase);

            return filter.ToString();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer Fireminds
Croatia Croatia
Sanjin Matusan - C#, WPF, SQL, .NET

Comments and Discussions