Home > Enterprise >  How can I use filters if my TcxGrid is on GridMode True in Delphi
How can I use filters if my TcxGrid is on GridMode True in Delphi

Time:09-17

I want to be capable to filter the records loaded in my TcxGrid component, I have GridMode because the grid is kinda slow to show the records on my application form, but this mode doesn't let me use the filters of the column (just the default ones).

enter image description here

I found this on the oficial forums:

Filtering can be implemented by setting the Data Controller's Filter.AutoDataSetFilter property value to True. This way, the filter expression will be automatically applied to the underlying dataset. In the meantime, please note that the Grid working in Grid mode, does not create a list of the possible filter items in the column dropdown filter list. However, you may implement this yourself within the column's OnGetFilterDisplayText or OnGetFilterValues events or the Data Controller's OnGetValueList event. Please refer to the ExpressQuantumGrid's documentation to get more information about these events and how to use them.

But I can't figure out how to do that, since I can't find those documentation. Any help will be appreciated

CodePudding user response:

DevExpress has a great support team, contact them if you need help with their products.

Anyhow, broadly, it works as follows: You can freely add values by code using the column's OnGetFilterValues event:

procedure TForm1.cxGrid1DBTableView1MYFIELDGetFilterValues(
  Sender: TcxCustomGridTableItem; AValueList: TcxDataFilterValueList);
begin
  AValueList.Add(fviValue, 'Value A', 'Value A', False);
  AValueList.Add(fviValue, 'Value B', 'Value B', False);
  AValueList.Add(fviValue, 'Value C', 'Value C', False);
end;

Picture showing the filter values dropdown

Setting the DataController's Filter.AutoDataSetFilter causes the underlying TDataSet.Filter property to be automatically filled with the corresponding condition. For example:

Picture showing the filter values dropdown with Value A and Value B elements selected

ShowMessage(cxGrid1DBTableView1.DataController.DataSet.Filter);

((MYFIELD = 'Value A') OR (MYFIELD = 'Value B'))

It obviously depends on how the TDataSet's descendant class you're using reacts when the Filter's property changes.

If you need more flexibility, you can use the DataController.Filter.OnBeforeChange or the DataController.Filter.OnChanged event handlers.

  • Related