KB Article T904977
Visible to All Users

Filter DevExpress Data-Aware Controls - WinForms Cheat Sheet

Other DevExpress WinForms Cheat Sheets


Most DevExpress Data-Aware controls (Data Grid, Tree List, Vertical Grid, Gantt Control) support a similar filter UI and API. For detailed information on how to filter data in data-aware controls, refer to the corresponding control's help topics:

To implement a custom filtering, you can handle the ~CustomRowFilter event:
Data Grid Views - CustomRowFilter
TreeList & GanttControl - CustomRowFilter
VGridControl - CustomRecordFilter

Examples

How to apply a predefined filter on a button click?

Data-aware controls support the ActiveFilterString, ActiveFilterCriteria and ActiveFilter properties that apply filter criteria in code. Set this properties inside a button click event handler.

C#
private void showActiveCustomersBtn_Click(object sender, EventArgs e) { gridView.ActiveFilterString = "IsActive = true"; }

Alternatively you can use FilterInfo (GridColumn.FilterInfo, TreeListColumn.FilterInfo, VGridRow.Properties.FilterInfo) to specify filter condition.

C#
GridColumn columnCustomer = gridView1.Columns["CustomerID"]; columnCustomer.FilterInfo = new ColumnFilterInfo("[ShipCountry] = 'Poland'");

Note that if you need to set up Sorting, Grouping or Filtering settings of a GridColumn (FilterInfo, SortOrder, GroupIndex etc), these changes are applied only after this column is added to the ColumnView.Columns collection.

How to apply a static filter that end-users cannot change?

There are several solutions for this task. First, since data-aware controls show data "as-is", the simplest solution is to filter your data source or load only required records from your database by executing the corresponding SQL query.
If this approach is not suitable, you can use the GridView.CustomRowFilter / TreeList.CustomRowFilter / VGridControl.CustomRecordFilter event to apply static filtering.

C#
private void GridView_CustomRowFilter(object sender, DevExpress.XtraGrid.Views.Base.RowFilterEventArgs e) { GridView view = sender as GridView; bool isCustomerActive = (bool)view.GetListSourceRowCellValue(e.ListSourceRow, "IsActive"); e.Visible = e.Visible && isCustomerActive; e.Handled = true; }

In addition to these events, you can use the SubstituteFilter event to extend the applied filter.

How to implement a stand-alone editor that filters data-aware control's data depending on text entered by an end-user?

Drop a SearchControl onto your form and pass a data-aware control to the SearchControl.Client property. See this help article for a complete list of controls compatible with the Search Control: SearchControl.

To combine the Search Control and custom filters for Data Grid, handle the SubstituteFilter event.

C#
private void GridView_SubstituteFilter(object sender, DevExpress.Data.SubstituteFilterEventArgs e) { e.Filter = e.Filter & yourFilterCriterion; criteriaOperator = e.Filter; }
Can I hide certain options from a standard Excel-style filter menu?

Handle the FilterPopupExcelData event to remove default or add custom entries to filter menus.

C#
private void ResourcesTree1_FilterPopupExcelData(object sender, FilterPopupExcelDataEventArgs e) { if(e.Column.FieldName == "Caption") { e.ClearData(); foreach (Resource resource in schedulerDataStorage1.Resources.Items) { if (resource.ParentId != null) e.AddData(resource.Caption, resource.Caption); } } }
How to hide certain conditions in Automatic Filtering Row condition selector?

You can manage these menu items in the GridView.PopupMenuShowing event - you can just remove required items from the e.Menu.Items collection.
For example, remove all items except "Contains", "Begins With", and "End With":

C#
private void GridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) { if(e.MenuType == GridMenuType.AutoFilter) { for(int i = e.Menu.Items.Count - 1; i >= 0; i--) { ColumnAutoFilterCondition menuTag = (ColumnAutoFilterCondition)e.Menu.Items[i].Tag; if(menuTag != ColumnAutoFilterCondition.BeginsWith && menuTag != ColumnAutoFilterCondition.Contains && menuTag != ColumnAutoFilterCondition.EndsWith) e.Menu.Items.RemoveAt(i); } } }

See Also:

Help us improve this article

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.