FNCDataGrid Filter Popup

Is it possible to programmatically get and set the checked status of items in the filter popup of the FNCDataGrid?

image

uses
  ...
  // Add:
  , VCL.TMSFNCCheckedListBox

...
type TMyForm = class(TForm)
...
  // Add:
  private
    FLastProcessedFilterColumn : integer;

...
...

procedure TMyForm.MyDataGridAfterShowFilter(Sender: TObject; AColumn: Integer);
var
  LFilterList : TTMSFNCCheckedListBox;
  LItem : TCollectionItem;
  LCurrentItem : TTMSFNCCheckedListBoxItem;
begin
  if AColumn <> FLastProcessedFilterColumn then begin
    FLastProcessedFilterColumn := AColumn;
    MyDataGrid.BeginUpdate;
    LFilterList := MyDataGrid.root.FilterListBox;
    LFilterList.BeginUpdate;
    for LItem in LFilterList.Items do begin
      LCurrentItem := TTMSFNCCheckedListBoxItem(LItem);
      if AColumn = 1 then begin
        //replace below 'if' with any other conditions you need to make items checked/unchecked
        if LCurrentItem.Text = 'What I''m looking for' then begin
          LCurrentItem.Checked := false;
          LFilterList.OnItemCheckChanged(LFilterList,LCurrentItem);
        end;
      end else if AColumn = 2 then begin
        //replace below 'if' with any other conditions you need to make items checked/unchecked
        if LCurrentItem.Text = 'What I''m looking for in another column' then begin
          LCurrentItem.Checked := false;
          LFilterList.OnItemCheckChanged(LFilterList,LCurrentItem);
        end;
      end;
    end;
    LFilterList.EndUpdate;
    MyDataGrid.EndUpdate;
  end;
end;

Note that this FLastProcessedFilterColumn intends to preserve some manual user change of filtering that could check something that had been automatically unchecked by you within this method.

Hope it helps. Works in not too complicated scenarios.

Thanks for the attempt :) My use case requires the interaction to happen outside the grid event structure though. I would like to update the selection without opening the popup and have the changes persist if the user then chooses to open the popup and make manual selections from the list of choices.

Hi, alternatively, you can build the filter with

TMSFNCDataGrid1.ClearFilter;
fd := TMSFNCDataGrid1.Filter.Add(3, gftEqual, '"Abroad"|"Office"');
TMSFNCDataGrid1.ApplyFilter;

image

1 Like

Thanks Pieter, that will meet my needs.

1 Like

Hi!

These way you're clearing all column filters, and then setting an especific column active filters.

  • What would happen to other columns user selection filters?
  • Is there a way to get a column filter item checked status outside of any of the filters events (and setting them without changing other columns filters) ?

The code should be optimized if that is a requirement. Programmatically changing an existing column filter can be done with

var
  fd: TTMSFNCDataGridDataFilterData;
begin
  fd := TMSFNCDataGrid1.Filter.ColumnFilter[3];
  fd.Condition := '"Abroad"|"Office"';
  fd.&Type := gftEqual;
end;

The code will automatically retrieve or add a filter depending on the active filter for that column. There is no need to call ClearFilter & ApplyFilter, that was just for showing a filter being active in the screenshot above.

Hi Pieter, I note that you say it is not necessary to call ApplyFilter, but all of my testing suggests that you must do this for the grid display to update. Is this correct or am I misunderstanding something? More so, it appears that unless the MultiColumn Option is enabled, you have to call ApplyFilter each time you update the filter active on a column, otherwise it will ignore the previous changes - Is that correct?

ApplyFilter is required to set and update the filter. What I meant it's not required to see the filter in the dropdown before applying it.