Skip to main content

Example: Changing Selected Records in Provider and Unbound Modes

  • 3 minutes to read

The following example shows how to access selected records in provider and unbound modes.

The total number of selected rows (data records and grouping rows) is determined by the DataController.GetSelectedCount function. To iterate selected rows in unbound and provider modes, the data controller’s GetSelectedRowIndex method must be used. It returns the row index of a specific selected row that can be used to retrieve information on the row using the GetRowInfo function.

Suppose that records of the data controller are displayed within a tvOrders view of the ExpressQuantumGrid control. The following code shows how to change the tvOrdersSaleDate column values of selected records to the current date.

var
  I: Integer;
  ARowIndex: Integer;
  ARowInfo: TcxRowInfo;
begin
  with tvOrders.DataController do
  begin
    BeginUpdate;
    try
      for I := 0 to GetSelectedCount - 1 do
      begin
        ARowIndex := GetSelectedRowIndex(I);
        ARowInfo := GetRowInfo(ARowIndex);
        //Test whether a row is a grouping row
        if ARowInfo.Level < Groups.GroupingItemCount then
          Continue
        else
          //unbound or provider mode
          Values[ARowInfo.RecordIndex, tvOrdersSaleDate.Index] := Now();
      end;
    finally
      EndUpdate;
    end;
  end;
end;

The following code demonstrates another approach to access selected records. The data controller provides the ForEachRow method to call a custom procedure (a callback function) automatically for each row. This method is overridden by the TcxDBDataController class, so you can use it in all loading modes (provider, unbound, bound, and grid mode).

The first parameter defines whether selected or all rows should be processed. The custom procedure to be called for each row, is passed as the second parameter of ForEachRow.

To use the ForEachRow method, you have to write a procedure with a specific prototype (see the method’s description). This procedure should perform operations on a row. In our example, it changes the tvOrdersSaleDate column value:

procedure TForm1.SetSaleDate(ARowIndex: Integer; ARowInfo: TcxRowInfo);
begin
  with tvOrders.DataController do
  begin
    //Test whether a row is a data record
    if ARowInfo.Level = Groups.GroupingItemCount then
      //unbound or provider mode
      Values[ARowInfo.RecordIndex, tvOrdersSaleDate.Index] := Now();
  end;
end;

The code that calls the SetSaleDate method for each row is shown below:

with tvOrders.DataController do
  begin
    BeginUpdate;
    try
      ForEachRow(True, SetSaleDate);
    finally
      EndUpdate;
    end;
  end;