Skip to main content

Example: TcxCustomDataController.FilteredRecordCount, TcxCustomDataController.FilteredRecordIndex

  • 2 minutes to read

The number of records that satisfy the applied filter criteria can be obtained via the FilteredRecordCount property.

The following example iterates filtered records in a tvUsers view of the ExpressQuantumGrid control and stores values of their key fields in a list. A key field is a unique field used to identify a dataset record.

After that, filtered records can be accessed by their key field values via the data controller’s LocateByKey or the dataset’s Locate methods. These methods make a particular record active, thus allowing you to change its field values.

The key field in the current example is identified by the ID field. To retrieve the ID field value, a filtered record is activated using the FocusedRecordIndex property. This property identifies the record index of the focused record. See the FocusedRecordIndex property for more information.

The record index of a specific filtered record is determined by the FilteredRecordIndex property.

var
  I: Integer;
  AKeyFieldValueList: TList;
//...
  with tvUsers.DataController do
  begin
    //Select records with tvUsersCITY column values
    //equal to 'Los Angeles'
    Filter.Clear;
    Filter.Root.AddItem(tvUsersCITY, foEqual, 'Los Angeles', 
      'Los Angeles');
    Filter.Active := True;
    AKeyFieldValueList := TList.Create;
    BeginUpdate;
    try
      //Copy key field values of filtered records to the list
      for I := 0 to FilteredRecordCount - 1 do
      begin
        FocusedRecordIndex := FilteredRecordIndex[I];
        AKeyFieldValueList.Add(Pointer(Integer(
          DataSet.FieldValues['ID'])));
      end;
      //do some operation on filtered records using key field values
      //...
    finally
      EndUpdate;
      AKeyFieldValueList.Free;
    end;
  end;