Example T501883
Visible to All Users

WinForms Data Grid - Customize the Expression Editor

This example shows how to customize the Expression Editor as follows:

  • Remove the Now function from the list of Date-Time functions and prevent users from using this function in expressions (display an error message).
  • Disable capitalization of function names.
  • Implement a color provider to use a custom palette for syntax highlighting.
  • Rename the Columns category to Fields.
  • Implement a custom Expression Editor view to display only specific UI elements of the Expression Editor.

Customize WinForms Expression Editor

Right-click the Expression column header and select the Expression Editor… command in the context menu to invoke the Expression Editor.

Show Expression Editor

Files to Review

Documentation

Does this example address your development requirements/objectives?

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

Example Code

WindowsFormsApplication1/Form1.cs(vb)
C#
using DevExpress.Data.Controls.ExpressionEditor; using DevExpress.Data.Filtering; using DevExpress.Data.Filtering.Helpers; using DevExpress.DataAccess.ExpressionEditor; using DevExpress.DataAccess.UI.ExpressionEditor; using DevExpress.DataAccess.UI.Native.ExpressionEditor; using DevExpress.LookAndFeel; using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; namespace WindowsFormsApplication1 { public partial class Form1 : XtraForm { public class Product { public Product(int productID, string productName, Category category) { ProductID = productID; ProductName = productName; Category = category; } public int ProductID { get; } public string ProductName { get; } public int? SupplierID { get; set; } public int? CategoryID { get; set; } public short? UnitsOnOrder { get; set; } public bool Discontinued { get; set; } public Category Category { get; } } public class Category { public int CategoryID { get; set; } public string CategoryName { get; set; } } static List<Product> GetProductsList() { var categoryBeverages = new Category { CategoryID = 1, CategoryName = "Beverages" }; var categoryConfections = new Category { CategoryID = 2, CategoryName = "Condiments" }; return new List<Product> { new Product(1, "Chai", categoryBeverages), new Product(2, "Chang", categoryBeverages), new Product(3, "Coffee", categoryBeverages), new Product(4, "Chocolade", categoryConfections), new Product(5, "Maxilaku", categoryConfections), new Product(6, "Valkoinen suklaa", categoryConfections), }; } public Form1() { InitializeComponent(); this.gridControl1.DataSource = GetProductsList(); } void gridView1_UnboundExpressionEditorCreated(object sender, DevExpress.XtraGrid.Views.Base.UnboundExpressionEditorEventArgs e) { if(e.ExpressionEditorContext == null) { return; } // Exclude "Now" from the list of available functions. var nowFunction = e.ExpressionEditorContext.Functions.FirstOrDefault(fi => string.Equals(fi.Name, "now", StringComparison.OrdinalIgnoreCase)); if(nowFunction != null) { e.ExpressionEditorContext.Functions.Remove(nowFunction); } // Uncomment the following line to use a custom color provider. //e.ExpressionEditorContext.ColorProvider = new CustomColorProvider(); // Implement a custom criteria validator (e.g., to forbid using a specific function). e.ExpressionEditorContext.CriteriaOperatorValidatorProvider = new ValidatorProvider(); // Disable capitalization of function names in expressions. e.ExpressionEditorContext.OptionsBehavior.CapitalizeFunctionNames = false; // Rename the "Columns" category to "Fields". foreach(var columnInfo in e.ExpressionEditorContext.Columns) { columnInfo.Category = "Fields"; } // Uncomment the following line to use a custom Expression Editor view. //e.ExpressionEditorView = new CustomExpressionEditorView(this.LookAndFeel, new CustomExpressionEditorControl()); } } class ValidatorProvider : ICriteriaOperatorValidatorProvider { #region Implementation of ICriteriaOperatorValidatorProvider public ErrorsEvaluatorCriteriaValidator GetCriteriaOperatorValidator(ExpressionEditorContext context) { return new Validator(context); } #endregion } class Validator : CriteriaOperatorValidator { public Validator(ExpressionEditorContext context) : base(context, supportsAggregates: true) { } #region Overrides of CriteriaOperatorValidator public override void Visit(FunctionOperator theOperator) { if (theOperator.OperatorType == FunctionOperatorType.Now) { this.errors.Add(new CriteriaValidatorError("Invalid function: now()")); } base.Visit(theOperator); } #endregion } class CustomColorProvider : IExpressionEditorColorProvider { public Color GetColorForElement(ExpressionElementKind elementKind) { if (elementKind == ExpressionElementKind.Column) return Color.BlueViolet; if (elementKind == ExpressionElementKind.Function) return Color.Brown; return Color.Azure; } } public class CustomExpressionEditorView : ExpressionEditorView { public CustomExpressionEditorView(UserLookAndFeel lookAndFeel, ExpressionEditorControl expressionEditor) : base(lookAndFeel, expressionEditor) { } } public class CustomExpressionEditorControl : ExpressionEditorControl { #region Overrides of ExpressionEditorControl protected override ExpressionDocumentationControl CreateDocumentationControl() { return null; } #endregion } }

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.