Example E3148
Visible to All Users

WinForms Data Grid - Implement new filter options in the Auto Filter Row

This example creates a custom grid control (MyGridControl) to introduce new filter options. Users can use the following operands to filter DateTime values in columns using the Auto Filter Row: "<", ">", ">=", "<=".

See the implementation of the GridView.CreateAutoFilterCriterion method:

C#
protected override DevExpress.Data.Filtering.CriteriaOperator CreateAutoFilterCriterion(DevExpress.XtraGrid.Columns.GridColumn column, DevExpress.XtraGrid.Columns.AutoFilterCondition condition, object _value, string strVal) { if (column.ColumnType == typeof(DateTime) && strVal.Length > 0) { BinaryOperatorType type = BinaryOperatorType.Equal; string operand = string.Empty; if (strVal.Length > 1) { operand = strVal.Substring(0, 2); if (operand.Equals(">=")) type = BinaryOperatorType.GreaterOrEqual; else if (operand.Equals("<=")) type = BinaryOperatorType.LessOrEqual; } if (type == BinaryOperatorType.Equal) { operand = strVal.Substring(0, 1); if (operand.Equals(">")) type = BinaryOperatorType.Greater; else if (operand.Equals("<")) type = BinaryOperatorType.Less; } if (type != BinaryOperatorType.Equal) { string val = strVal.Replace(operand, string.Empty); try { DateTime dt = DateTime.ParseExact(val, "d", column.RealColumnEdit.EditFormat.Format); return new BinaryOperator(column.FieldName, dt, type); } catch { return null; } } } return base.CreateAutoFilterCriterion(column, condition, _value, strVal); }

Files to Review

Documentation

See Also

Does this example address your development requirements/objectives?

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

Example Code

WindowsApplication3/CustomGrid.cs(vb)
C#
using System; using System.Collections.Generic; using System.Windows.Forms; using DevExpress.Skins; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Registrator; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Grid; using DevExpress.Data.Filtering; namespace DXSample { public class MyGridControl : GridControl { protected override BaseView CreateDefaultView() { return CreateView("MyGridView"); } protected override void RegisterAvailableViewsCore(InfoCollection collection) { base.RegisterAvailableViewsCore(collection); collection.Add(new MyGridViewInfoRegistrator()); } } public class MyGridViewInfoRegistrator : GridInfoRegistrator { public override string ViewName { get { return "MyGridView"; } } public override BaseView CreateView(GridControl grid) { return new MyGridView(grid as GridControl); } } public class MyGridView : GridView { public MyGridView() : this(null) { } public MyGridView(DevExpress.XtraGrid.GridControl grid) : base(grid) { } protected override string ViewName { get { return "MyGridView"; } } protected override DevExpress.Data.Filtering.CriteriaOperator CreateAutoFilterCriterion(DevExpress.XtraGrid.Columns.GridColumn column, DevExpress.XtraGrid.Columns.AutoFilterCondition condition, object _value, string strVal) { if (column.ColumnType == typeof(DateTime) && strVal.Length > 0) { BinaryOperatorType type = BinaryOperatorType.Equal; string operand = string.Empty; if (strVal.Length > 1) { operand = strVal.Substring(0, 2); if (operand.Equals(">=")) type = BinaryOperatorType.GreaterOrEqual; else if (operand.Equals("<=")) type = BinaryOperatorType.LessOrEqual; } if (type == BinaryOperatorType.Equal) { operand = strVal.Substring(0, 1); if (operand.Equals(">")) type = BinaryOperatorType.Greater; else if (operand.Equals("<")) type = BinaryOperatorType.Less; } if (type != BinaryOperatorType.Equal) { string val = strVal.Replace(operand, string.Empty); try { DateTime dt = DateTime.ParseExact(val, "d", column.RealColumnEdit.EditFormat.Format); return new BinaryOperator(column.FieldName, dt, type); } catch { return null; } } } return base.CreateAutoFilterCriterion(column, condition, _value, strVal); } } }

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.