KB Article K18333
Visible to All Users

How to force the GridLookUpEdit to filter rows and show only those that contain a specified sub-string at least in one of displayed columns

Description:
Currently, the GridLookUpEdit applies the automatic filtering feature using the Contains filter condition only based upon a field, specified by the RepositoryItemLookUpEditBase.DisplayMember property. I want to make the editor to filter rows in a similar way, but based upon all visible columns. So, if an entered string is contained in any of the columns for a specific row, this row should be displayed. How can I implement this functionality?

Answer:
The current GridLookUpEdit's implementation doesn't provide interfaces to force the described feature to work automatically. However, in this article, we'll try to create a GridLookUpEdit's descendant and introduce the required behavior.
To accomplish this task, we'll have to create GridLookUpEdit and GridView descendants.
To force the drop-down grid to apply a proper filter condition, we need to override the OnCreateLookupDisplayFilter method. Here is some sample code:

C#
public class CustomGridView : GridView { public CustomGridView() : base() { } protected override string OnCreateLookupDisplayFilter(string text, string displayMember) { string exp = LikeData.CreateContainsPattern(text); string searchString = ""; foreach (GridColumn col in Columns) { if (col.Visible) searchString = searchString + new BinaryOperator(col.FieldName, exp, BinaryOperatorType.Like).ToString() + " Or "; } searchString = searchString.Substring(0, searchString.Length - 4); return searchString; } protected virtual internal string GetExtraFilterText { get { return ExtraFilterText; } } }

Additionally, it is necessary to highlight all found matches. To do this, we should override the view's painting mechanism. This can be done by creating our custom painter and by overriding the DrawRowCell method:

C#
public class CustomGridPainter : GridPainter { public CustomGridPainter(GridView view) : base(view) { } public virtual new CustomGridView View { get { return (CustomGridView)base.View; } } protected override void DrawRowCell(GridViewDrawArgs e, GridCellInfo cell) { cell.ViewInfo.MatchedStringUseContains = true; cell.ViewInfo.MatchedString = View.GetExtraFilterText; cell.State = GridRowCellState.Dirty; e.ViewInfo.UpdateCellAppearance(cell); base.DrawRowCell(e, cell); } }

Now you should properly register it, to make it available. Please refer to the How to create a GridView descendant class and register it for design-time use article to learn more on how to accomplish this.
Finally you should create a GridLookUpEdit descendant, an override the RepositoryItemGridLookUpEdit.CreateViewInstance and RepositoryItemGridLookUpEdit.CreateGrid methods.
The attached example contains descendants of the GridView, GridPainter, GridLookUpEdit, RepositoryItemGridLookUpEdit classes.
Additionally, there are GridControl and GridInfoRegistrator descendants for registering a custom grid view.
See Also:
How to create a GridView descendant class and register it for design-time use
Custom Editors

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.