Adding More Complicated Filters to Your QDataGrid
This example will walk us through more advanced methods and types of filtering that can be done
on your QDataGrid. Make sure you've got a good handle on the first
example before you dive into these shark-infested waters.
Changing MetaColumn Filters
It's worth noting that since MetaColumns will automatically set filters for you, you may have to
clear them first before applying new ones of your own configuration. To do so, just set the column's
FilterType to None before setting up your new filter.
Dropdown Lists
In Example A's QDataGrid, we've added a column filter that's a list. This is easily accomplished
using FilterAddListItem and setting the FilterType to QFilterType::ListFilter.
This example is filled from the Project type table, which is almost certainly small enough to avoid
any performance problems. You could theoretically do the same with the First Name column, by
populating the filter list with the entire collection of first names, but to do so you'd have to
load all the people records into memory to build that list and then you're still forcing the user to
find the one they want in that giant list, so a filter would lose any real performance or usability
benefits it had.
Filter Constants
There are rare occasions where you need another filter to apply in addition to any the user supplies.
So for example, you want to add a QQ::Equal filter that doesn't depend on the user's input
into the filter textbox, but only when there's actually something supplied for that textbox.
In Example A, the Last Name filter only considers users who also have an enabled log in.
Custom Filters
Filters aren't limited to just datagrids filled by using QQuery -- we've got support for custom
filters as well. This will allow you to use filters on QDataGrids filled from arrays, including
those created from custom SQL queries.
If you aren't using QQuery, you must use the FilterByCommand property of the
QDataGridColumn and the FilterInfo property of the QDataGrid instead of
Filter and Conditions respectively. The QDataGridColumn's FilterByCommand property
must take the form of an array. When retrieving the QDataGrid's FilterByCommand property, you
will receive an array of the applied column's FilterByCommands, with the user's input set at
each array's 'value' key.
As a result you can pull up that same information during your QDataGrid's Bind function in
order to perform the actual filtering on the columns the user has selected.
In Example B, we use raw SQL to allow a filter on an address count column.
Saving and Restoring Filter States
If you want to remember what filters a user has applied to a datagrid, you can use the GetFilters
and SetFilters functions to do so. Simply call GetFilters every time the contents of the datagrid changes
(such as during the data bind), and store that somewhere (like the session). Then, when you want to re-create
that state (such as at Form_Create), use SetFilters to re-apply them.