Example E5021
Visible to All Users

WinForms Data Grid - How to make the Auto Filter Row insensitive to accents

This example demonstrates how to create a custom function that removes diacritic symbols from the specified string. The GridView.SubstituteFilter event is handled to inject the custom function into the grid's active filter.

Note

The SubstituteFilter event is available in v15.1+. For older versions, create a custom grid control and customize the auto-filter row's behavior. You should override the GridView.RaiseCustomRowFilter method to normalize the cell text and filter string using the standard String.Normalize method. The cell value is processed based on the comparison operator type specified by the OptionsColumnFilter.AutoFilterCondition property.

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

DxSample/Filtering/GridFilterSubstitutor.cs(vb)
C#
using DevExpress.Data.Filtering; using DevExpress.Data.Filtering.Helpers; namespace DxSample.Filtering { public class GridFilterSubstitutor : ClientCriteriaLazyPatcherBase.AggregatesCommonProcessingBase { private static CriteriaOperator WrapIntoCustomFunction(CriteriaOperator param) { return new FunctionOperator(FunctionOperatorType.Custom, new ConstantValue("RemoveDiacritics"), param); } public static CriteriaOperator Substitute(CriteriaOperator source) { return new GridFilterSubstitutor().Process(source); } public override CriteriaOperator Visit(FunctionOperator theOperator) { if (theOperator.OperatorType == FunctionOperatorType.StartsWith || theOperator.OperatorType == FunctionOperatorType.EndsWith || theOperator.OperatorType == FunctionOperatorType.Contains) return new FunctionOperator(theOperator.OperatorType, WrapIntoCustomFunction(theOperator.Operands[0]), WrapIntoCustomFunction(theOperator.Operands[1])); return base.Visit(theOperator); } } }
DxSample/Filtering/RemoveDiacriticsFunction.cs(vb)
C#
using DevExpress.Data.Filtering; using DevExpress.Utils.Extensions; using System; using System.Globalization; using System.Linq; using System.Text; namespace DxSample.Filtering { public sealed class RemoveDiacriticsFunction : ICustomFunctionOperator { public object Evaluate(params object[] operands) { var src = (string)operands[0]; if (src == null) return string.Empty; var sb = new StringBuilder(); src.Normalize(NormalizationForm.FormKD) .Where(c => !IsDiacritic(c)) .ForEach(c => sb.Append(c)); return sb.ToString(); } private bool IsDiacritic(char symbol) { var category = CharUnicodeInfo.GetUnicodeCategory(symbol); return category == UnicodeCategory.NonSpacingMark || category == UnicodeCategory.SpacingCombiningMark || category == UnicodeCategory.EnclosingMark; } public string Name => "RemoveDiacritics"; public Type ResultType(params Type[] operands) => typeof(string); } }
DxSample/MainForm.cs(vb)
C#
using System; using System.Collections.Generic; using System.Data; using System.Linq; using DevExpress.Data; using DevExpress.XtraEditors; using DxSample.Filtering; namespace DxSample { public partial class MainForm : XtraForm { public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { DataSet ds = new DataSet(); ds.ReadXml("nwind.xml"); DataTable dt = ds.Tables["Customers"]; var customers = from r in dt.Rows.Cast<DataRow>() let required = new string[] { "ANTON", "BERGS", "BLONP", "BOLID", "COMMI", "FOLKO", "GALED", "GODOS", "HILAA" } where required.Contains((string)r["CustomerID"]) select new { CompanyName = (string)r["CompanyName"], ContactName = (string)r["ContactName"], ContactTitle = (string)r["ContactTitle"] }; this.GridControl.DataSource = customers.ToList(); } private void GridView_SubstituteFilter(object sender, SubstituteFilterEventArgs e) { e.Filter = GridFilterSubstitutor.Substitute(e.Filter); } } }
DxSample/Program.cs(vb)
C#
using DevExpress.Data.Filtering; using DevExpress.LookAndFeel; using DevExpress.Skins; using DevExpress.UserSkins; using DxSample.Filtering; using System; using System.Windows.Forms; namespace DxSample { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); BonusSkins.Register(); SkinManager.EnableFormSkins(); UserLookAndFeel.Default.SkinName = "Lilian"; CriteriaOperator.RegisterCustomFunction(new RemoveDiacriticsFunction()); Application.Run(new MainForm()); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.