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
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.
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.
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;
}
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);
}
}
}
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:
- How to filter a second LookUp column based on a first LookUp column's value
- How to filter or use different choices for each combo box cell in the Grid