Skip to main content

Expand and Collapse Rows

  • 4 minutes to read

The ExpressQuantumGrid control displays records obtained from a data source with the help of a View object. It provides four distinct types of Views representing records in a specific manner:

  • a Table View arranges dataset records in rows each of which can be either of four types:

  • a grid data row (TcxGridDataRow) displaying field values of a dataset record;

  • a grid group row (TcxGridGroupRow) is a row displaying the value of a column by which data is grouped as well as any group summaries;

  • a grid master data row (TcxGridMasterDataRow) is a data (non-group) row in a master View;

  • the “new item row” (TcxGridNewItemRow) represents the row for entering new data;

  • a Banded Table View also represents dataset records as rows, but it is capable of arranging columns in bands. The row types in a Banded Table View are the same as in a Table View.

  • a Card View arranges records in cards each of which represents an object of the TcxGridCard class.

  • a Chart View represents each dataset record as category and series values (which correspond to field values) within this category. Various diagrams then render categories and series to produce charts. You can aggregate data within categories using data groups. Refer to the Creating Data Groups in Chart Views topic for additional information.

A Table View, Banded Table View and Card View reference displayed rows/cards via their ViewData property. This property provides indexed access to all records which are currently visible or potentially visible by scrolling. Records hidden under collapsed groups within tabular Views are not accessible via ViewData. To access individual rows/cards by the record index, use the View’s ViewData.Records property (you can refer to the Records topic to know more about record objects).

A Chart View references the items (categories and series values) via the ViewData.Categories and ViewData.Values properties. You can refer to the Obtain and Set Values in Chart Views topic for details.

Expand/collapse group & master rows in (Banded) Table Views

Expanding/collapsing group & master rows can be performed in a number of ways:

  • the Expanded property of a record object. It allows expanding/collapsing group & master rows:

The following code sets the Expanded property of the first record within the tvCustomers View to True. If this record is either a group or master row, it will be expanded. Otherwise, nothing happens.

tvCustomers.ViewData.Records[0].Expanded := True;

Similar to the Expanded property, these methods allow you to expand group and master rows. However, the Expand and Collapse methods enable you to perform recursive expansion/collapse of group rows. If data is grouped by multiple columns, you can expand all nesting groups under the first row by using the following code:

tvCustomers.ViewData.Records[0].Expand(True);

Passing False as the Expand method parameter expands only the given row.

Suppose you have two Views representing a master-detail relationship, data within a master View being grouped by one or more columns. Calling the Expand or Collapse method with True as a parameter for group rows within the master View does not expand/collapse nested master data rows.

The DoRecursiveExpansion procedure enables you to work around this problem. It performs recursive expansion for the record specified by the ARecordIndex parameter within the AView View. At first, it expands the given row. Any nested rows, which have appeared, are also expanded.

procedure DoRecursiveExpansion(AView: TcxGridTableView; ARecordIndex: Integer);
var
  APrevRecordCount, ANestedRecordCount: Integer;
begin
  with AView.ViewData do
  begin
    APrevRecordCount := RecordCount;
    //Expand the given record
    Records[ARecordIndex].Expand(True);
    ANestedRecordCount := RecordCount  - APrevRecordCount;
    //Process nested records which have appeared
    while ANestedRecordCount > 0 do
    begin
      Inc(ARecordIndex);
      Records[ARecordIndex].Expand(True);
      Dec(ANestedRecordCount);
    end;
  end;
end;

These methods expand/collapse all group & master rows within a given View recursively. They take the ARecurse parameter which specifies whether an expansion/collapse applied to a master View affects all corresponding detail Views.

The next code expands records within the tvCustomers View and all detail Views linked to it:

tvCustomers.ViewData.Expand(True);
See Also