Creating And Deleting Columns
- 2 minutes to read
This topic covers the steps required to create and delete columns within the TreeList control.
Data-aware, unbound, and virtual TreeList controls provide a Columns property maintaining the collection of columns created within the control. This property returns:
A TcxTreeListColumn object in unbound and virtual TreeList controls.
A TcxDBTreeListColumn object in data-aware TreeList controls.
The only difference between these two objects is the DataBinding property.
Adding Columns Manually
This first method is applicable to all TreeList controls.
var
AColumn: TcxTreeListColumn;
ADBColumn: TcxDBTreeListColumn;
//...
begin
//...
//create an unbound column and add it to the first band
AColumn := ATreeList.CreateColumn;
AColumn.Position.BandIndex := 0;
//the same code, but it will work if you already have a band object in your TreeList control
AColumn := ATreeList.CreateColumn(ATreeList.Bands[0]);
//create a DB column and assign the valid field name
ADBColumn := TcxDBTreeListColumn(ADBTreeList.CreateColumn);
ADBColumn.Position.BandIndex := 0;
ADBColumn.DataBinding.FieldName := 'FirstName';
//...
end;
Adding Columns Within a Data-Aware TreeList Control
A data-aware TreeList control’s data controller provides the CreateAllItems method that allows you to create columns based on fields in the linked dataset. This method binds each created column to the corresponding dataset field via the DataBinding property.
cxDBTreeList1.DataController.CreateAllItems;
At design time, you can accomplish this via the control’s context menu or the Component Editor. Connect the TcxDBTreeList control to a valid data source, and do one of the following:
Right-click the TreeList control and select the Create All Columns item from the context menu.
Invoke the Component Editor, switch to the Columns tab, and click the Create all fields button.
Deleting Columns
To delete a column at runtime, you just need to invoke the column’s destructor. The following code is used to dispose of the first column within the TreeList control:
cxTreeList1.Columns[0].Free;
The DeleteAllColumns method allows you to delete all columns in the control.
tvPersons.DeleteAllColumns;
At design time, the Component Editor allows you to delete individual columns. Switch to the Columns tab, select the columns to delete, and click the Delete button. You can select multiple columns to delete them at once.
To delete all columns, you can right-click the TreeList control and select the Delete All Columns item from the context menu.