Click here to Skip to main content
Click here to Skip to main content

Automatic WPF Toolkit DataGrid Filtering

, 21 May 2010 BSD
Rate this:
Please Sign up or sign in to vote.
This article discusses a component that enables automated content filtering for the WPF Toolkit DataGrid.
Automated content filtering for the WPF Toolkit DataGrid - demo application


The article discusses a component that enables automated content filtering for the DataGrid (WPF Toolkit DataGrid or System.Windows.Controls.DataGrid). The inspiration was the article "DataGrid with built-in filter functionality" (for Windows Forms) that was also published on CodeProject.

A specific characteristic of this component is that it is not an inherited DataGrid control, but instead a new style is made for the DataGrid's header. That makes future upgrades easier, and increases compatibility with the DataGrid control.

Using this component is extremely simple, you only need to set a new style for the DataGrid header: ColumnHeaderStyle.

With the release of .NET Framework 4.0, DataGrid has become part of the framework, so there is also a Visual Studio 2010 solution without WPFToolkit.dll.


The component uses the LINQ Dynamic Query Library to build up the query string for DataGrid filtering. You should be familiar with the WPF concept of binding, templates, and styles.

Using the Code

Use of the component should be easy. You first have to add a reference to the DataGrid component and for the DataGridFilterLibrary project (or DLL if you wish). For .NET 4.0, add a "PresentationFramework.dll" reference, and for .NET 3.5, add a "WPFToolkit.dll" reference.

Then, in the XAML code where you have the DataGrid, you have to add the xmlns attribute to the root element of the markup file (namespaces wpftoolkit and filter).

Note that for a project that targets .NET 4.0, there is no need to add the WPF Toolkit reference.

<Window x:Class="DataGridFilterTest.Window1"

Finally, you have to set a custom header style for the DataGrid:

<DataGrid ColumnHeaderStyle="{StaticResource {ComponentResourceKey 
           TypeInTargetAssembly={x:Type filter:DataGridHeaderFilterControl}, 

Please note that if you want to use shared resources from a Custom Control Library, you must use the ComponentResourceKey markup extension to reference the resource in the DataGridFilterLibrary.

Using DataGridComboBoxColumn

The filter for distinct values (see the EmployeePosition type in the demo project) can be distinct, or the user can enter any text in the TextBox. In the latter case, the search is executed as a text search with the LIKE operator. This behaviour is adjusted using the attached property IsTextFilter.


<DataGridComboBoxColumn Header="Position (List Filter)"
    SelectedItemBinding="{Binding Path=Position}"
    ItemsSource="{Binding Source={StaticResource EmployeeData}, Path=EmployeePositionList}" 

The filter control for this DataGridComboBoxColumn is going to be a simple TextBox instead of the ComboBox with all-possible values for the employee position (see the column "Position - List Filter" in the demo project).

This is handy when the list has many items, e.g., 100 employee positions.

How Text Search Works

All text searches through the <textbox> are by default case insensitive searches (as the LIKE SQL clause). The filter control also supports wildcard character % operators.

Configuration Options

Configuration is implemented through attached properties. See the following classes: DataGridColumnExtensions, DataGridComboBoxExtensions, and DataGridExtensions.


  • When applied to the combobox column, the filter combobox is fully editable, i.e., the user can type part of the word and the combobox automatically selects the first appropriate item (like System.Windows.Controls.TextSearch).
  • Default is false.
  • For example, see Position (Text Filter) column.


    Header="Position (List Filter)"
    <!-- The rest was left out to keep it more simple -->


  • When applied to the text column, search is case sensitive.
  • Default is false.
  • For example, see Email (Case Sensitive Search) column.


<DataGridTextColumn Binding="{Binding Path=Email}"
    Header="Email (Case Sensitive Search)"/>


  • For numeric and date columns, there is the option to filter between, i.e., that gives you the ability to specify a range in your search.
  • Default is false.
  • For example, see the Work Experience (Between) and Date Of Birth (Between) columns.


<DataGridTextColumn Header="Work Experience (Between)" 
    Binding="{Binding Path=WorkExperience}"/>


  • When applied to the datagrid, searches are performed on a separate thread using the BackgroundWorker.
  • Default is false.
  • For example, see the grid on the second tab item (myGrid2).


<DataGrid filter:DataGridExtensions.UseBackgroundWorkerForFiltering="True" 
    <!-- The rest was left out to keep it more simple -->


  • Gives the possibility to switch off the filter for a single column (useful for columns that do not need a filter, e.g., button columns).
  • For example, see the first grid where the filter for the column "Id" is hidden.


<DataGridTextColumn filter:DataGridColumnExtensions.DoNotGenerateFilterControl="True" 
    <!-- The rest was left out to keep it more simple -->


  • Shows/hides the "Clear filter" button.
  • Default is true.
  • For example, see the first grid where the clear filter button is hidden.


    <!-- The rest was left out to keep it more simple -->

Control Commands


It is not really a command, but an attached property. The property controls the visibility of the filter. It can be bound to a toolbar item. For example, see the "Show/Hide Filter (bind to attached property)" toolbar item.

<CheckBox IsChecked="{Binding Path=(filter:DataGridExtensions.IsFilterVisible),
    Show/Hide Filter (bind to attached property)


Clears the filter content and resets the filter. For example, see the "Clear Filter" toolbar item.

<Button Command="{Binding Path=(filter:DataGridExtensions.ClearFilterCommand), 
    Clear Filter

Please note that in the above case, we are binding to the attached properties. Syntax for this is in the form: Path=(namespace prefix:class name, surrounded by parentheses), e.g.:



The filter library internally uses (also indicated in the code):

  • Delay Textbox
  • (My WPF port of the Windows Forms version: - for smooth and responsive performance, thus simulating a kind of incremental search. For example, if the user types letters "A-B-C" in intervals less than 250 milliseconds, a search for an ABC text is performed, not 3 searches for A, AB, ABC.

  • EnumDisplayer
  • For better handling of enum values of the filter (

  • DataGridComboBoxColumn vs. DataGridComboBoxColumnWithBindingHack
  • To show the data in the combo box, the previous version used the DataGridComboBoxColumnWithBindingHack class. The solution for the modified combo column can be found here.

    Now I am using ObjectDataProvider as a binding source, so the ComboBox.ItemsSource binds to the EmployeePositionList through a StaticResource. For more information, see: WPF DataGrid – Dynamically updating DataGridComboBoxColumn. For this reason, the DataGridComboBoxColumn class is used instead of DataGridComboBoxColumnWithBindingHack.

    The example usage (both) is shown in the XAML code of the test project.

  • Start inserting Employees with new position
  • When pressed, every second, an employee (also with a new Position, i.e., Position 1, Position 2, Position 3 ...) is inserted in the EmployeeList list. If DataGrid.ItemsSource is an observable collection (source implements INotifyCollectionChanged) and a filter is set, the filter detects a collection change and then re-executes the filtering. Try and set the letter "p" in the filter for the "Position (Text Filter)" column and then press the button.



  • Added DoNotGenerateFilterControl configuration options (see chapter: Configuration options)
  • Filter detects collection change (only if source implements INotifyCollectionChanged) and then re-executes the filtering
  • (Some) Bug fixes - all bugs were found, thanks to the posted messages Smile | :)


  • Thanks to Bob Ranck and jsh_ec for their proposals and suggestions
  • Added the between control for date and numeric fields (option)
  • Added various configuration options (see chapter: Configuration options)
  • Added a clear filter command and a property that controls filter visibility
  • Now the filter adds a blank combobox item at the beginning of each list - quick and easy filter reset
  • Internal code revision


  • This is the first version. There are places for improvements in the internal design and functionality of the code components. Suggestions and comments are welcome.


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


About the Author

Software Developer Novatec (
Croatia Croatia
Sanjin Matusan - C#, WPF, SQL, .NET

Comments and Discussions

QuestionResize Columns problem Pinmemberltuttini24-Feb-13 18:29 
AnswerRe: Resize Columns problem Pinmembersmatusan25-Feb-13 11:45 
BugNullReferenceException when filtering after hiding and showing column PinmemberMember 92689006-Feb-13 4:24 
GeneralRe: NullReferenceException when filtering after hiding and showing column Pinmembersmatusan25-Feb-13 12:00 
QuestionUsing Filter from User Control Pinmemberfanievdw29-Jan-13 12:03 
AnswerRe: Using Filter from User Control Pinmembersmatusan25-Feb-13 12:03 
Question.NET 3.5 Pinmembersprintz29-Nov-12 20:05 
AnswerRe: .NET 3.5 Pinmembersprintz29-Nov-12 20:19 
GeneralRe: .NET 3.5 Pinmembersprintz29-Nov-12 20:46 
QuestionFilter is not setting properly Pinmemberrajeev_6-Nov-12 20:32 
AnswerRe: Filter is not setting properly Pinmembersmatusan25-Feb-13 12:13 
BugDisable Add/Delete/Edit while filter is applied PinmemberFurer Alexander28-Oct-12 5:13 
GeneralRe: Disable Add/Delete/Edit while filter is applied Pinmembersmatusan31-Oct-12 5:55 
QuestionGetting a Token Error [modified] PinmemberJohn Mike H.25-Oct-12 13:12 
Great tool thanks.
But having problem in doing a filter on a Combobox column. I changed it over to using the IsTextFilter="True" but still gives me an error.
I'm getting an error in the DynamicLibrary.ValidateToken procedure. It goes into this procedure at least 10 times right after I enter a filter value and on the 10th hit, I get an "Identifier Expected" error. My output window is showing a exception "System.Linq.Dynamic.ParseException".
I finally figured out what was needed to correct for this. In the DataGridColumnFilter.getValuePropertyBindingPath() I found that it was appending either the DisplayMemberPath or SelectedValuePath to the current path.
In mine, both of these values are empty. (I don't know if I'm doing it correctly since I'm still pretty new to WPF.) This was producing a query string for me like:
"ClientContract != null AND ClientContract.Category != null AND ClientContract.Category.CategoryName != null AND ClientContract.Category.CategoryName. != null AND ClientContract.Category.CategoryName."
I was able to get around this by just adding the conditional statements around them to check if they are empty before appending them as:
                            if (DataGridComboBoxExtensions.GetIsTextFilter(comboColumn))
                                if (comboColumn.DisplayMemberPath != "")
                                    path += "." + comboColumn.DisplayMemberPath;
                                if (comboColumn.SelectedValuePath != "")
                                    path += "." + comboColumn.SelectedValuePath;
With this change, so far everything seems to be working great for me.
Thanks again for the great tool.

modified 26-Oct-12 13:11pm.

AnswerRe: Getting a Token Error Pinmembersmatusan31-Oct-12 5:56 
QuestionHow to use it ? Pinmemberrajeev_25-Oct-12 1:25 
AnswerRe: How to use it ? Pinmembersmatusan25-Oct-12 2:20 
GeneralRe: How to use it ? [modified] Pinmemberrajeev_25-Oct-12 2:33 
Questionis it possible to filter with "contains" logic rather than "startswith" Pinmemberdavikokar24-Oct-12 10:35 
AnswerRe: is it possible to filter with "contains" logic rather than "startswith" Pinmembersmatusan24-Oct-12 21:49 
BugLost new row when filter is on PinmemberDieuzorro28-Sep-12 5:15 
GeneralMy vote of 5 Pinmember_avk25-Sep-12 20:35 
GeneralRe: My vote of 5 Pinmembersmatusan26-Sep-12 11:48 
QuestionException when sorting Position column Pinmemberfluflu4-Sep-12 23:47 
AnswerRe: Exception when sorting Position column Pinmembersmatusan26-Sep-12 11:43 
SuggestionDateTime comparison problem PinmemberKanwal Shehzad29-Aug-12 1:38 
GeneralRe: DateTime comparison problem PinmemberMarkOBrien533318-Oct-12 8:40 
QuestionSilverlight Version of DLL PinmemberYerpie25-Jul-12 10:18 
AnswerRe: Silverlight Version of DLL Pinmembersmatusan30-Jul-12 0:12 
BugParse error with GUID value PinmemberDieuzorro15-Jun-12 5:44 
GeneralRe: Parse error with GUID value Pinmembersmatusan17-Jun-12 7:34 
QuestionThis is very nice Pinmemberaprole13-Jun-12 12:38 
AnswerRe: This is very nice Pinmembersmatusan13-Jun-12 13:05 
QuestionRe: This is very nice [modified] Pinmemberaprole14-Jun-12 5:01 
AnswerRe: This is very nice Pinmembersmatusan17-Jun-12 7:36 
GeneralRe: This is very nice Pinmemberrajeev_6-Nov-12 20:22 
QuestionThanks alot for a great control! [modified] Pinmemberola121013-May-12 20:56 
GeneralMy vote of 5 Pinmembermantap8-May-12 21:50 
QuestionGrouping ??? PinmemberFirpas3-May-12 0:28 
AnswerRe: Grouping ??? Pinmembersmatusan17-Jun-12 7:41 
Questionloosing filtered view collection on button click PinmemberMember 31427479-Apr-12 11:39 
AnswerRe: loosing filtered view collection on button click Pinmembersmatusan11-Apr-12 6:14 
AnswerRe: loosing filtered view collection on button click PinmemberMember 314274711-Apr-12 11:38 
GeneralRe: loosing filtered view collection on button click Pinmembersmatusan11-Apr-12 13:26 
QuestionQuery String [modified] PinmemberGabriel Magno8-Mar-12 8:56 
AnswerRe: Query String Pinmembersmatusan22-Mar-12 1:24 
QuestionCannot use filter when no elements in datagrid - scroll is not visible Pinmemberbubijohn11116-Mar-12 0:30 
AnswerRe: Cannot use filter when no elements in datagrid - scroll is not visible Pinmembersmatusan22-Mar-12 1:29 
QuestionNot working for observable collection of observable Dicitonary PinmemberChaitali Desai23-Feb-12 20:55 
AnswerRe: Not working for observable collection of observable Dicitonary Pinmembersmatusan22-Mar-12 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 21 May 2010
Article Copyright 2009 by smatusan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid