Skip to main content

Example: TcxDBDataController.GetSelectedBookmark

  • 2 minutes to read

This example shows how you can iterate selected records in grid mode. In this mode, a data controller loads only a fixed amount of records from a dataset into memory at a time thus providing better performance with huge datasets (data grouping is not supported in grid mode). If you group data, grid mode is automatically disabled and the data controller will load all records from the underlying dataset to enable data grouping. To test whether grid mode is currently enabled, the IsGridMode function can be used.

In grid mode, the data controller automatically creates a bookmark (a TBookmarkStr object) for every selected record. You can retrieve these bookmarks via the GetSelectedBookmark function and then use them to make a particular dataset record active with the help of the TDataSet.Bookmark property. The number of selected records is determined by the GetSelectedCount function.

The following code shows how to access selected records displayed by a tvUsers view in the ExpressQuantumGrid control. The dataset displayed by the view contains an EMail field. For every selected record, an e-mail is sent to the address specified by the EMail field.

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  I: Integer;
  AEmail, ATextToSend, ASubject: string;
begin
  ATextToSend := 'Message text';
  ASubject := 'Subject';
  with tvUsers.DataController do
  begin
    if not IsGridMode then
    begin
      ShowMessage('Grid mode is disabled');
      Exit;
    end;
    BeginUpdate;
    try
      for I := 0 to GetSelectedCount - 1 do
      begin
        //Grid mode
        DataSet.Bookmark := GetSelectedBookmark(I);
        AEmail := DataSet.FieldValues['EMail'];
        SendEmail(AEmail, ASubject, ATextToSend);
      end;
    finally
      EndUpdate;
    end;
  end;
end;
procedure SendEmail(AEmail, ASubject, ATextToSend: string);
begin
  //...
end;