TAdvColumnGrid

example 2 : Creating inherited components from TAdvColumnGrid with extended column properties

Extending the TGridColumnItem class with a few extra properties and making these available in an descendent component and thus at design time is easy.

Everything starts with the TGridColumnCollection class GetItemClass method. This function returns the TGridColumnItem class that is used to hold all properties of a column.
When overriding this method and returning a descendent TGridColumnItem class, the collection starts using the new TGridColumnItem class that can thus have extra properties.

It is thus key to create a descendent class of the base TGridColumnCollection and override the GetItemClass function :

TGridColumnCollectionEx = class(TGridColumnCollection)
private
public
  function GetItemClass: TCollectionItemClass; override;
end;

with :

function TGridColumnCollectionEx.GetItemClass: TCollectionItemClass;
begin
  Result := TGridColumnItemEx;
end;

The new class that controls the grid's column properties descends from TGridColumnItem and is defined as :

TGridColumnItemEx = class(TGridColumnItem)
end;


The work is finished by making sure that the new TAdvColumnGrid is using the new TGridColumnCollectionEx instead of the TGridColumnCollection class.
This is done by creating a descendent class from TAdvColumnGrid and overriding the CreateColumns function and exposing the new Columns collection :

TAdvColumnGridEx = class(TAdvColumnGrid)
private
  function GetColumnsEx: TGridColumnCollectionEx;
  procedure SetColumnsEx(const Value: TGridColumnCollectionEx);
protected
  function CreateColumns: TGridColumnCollection; override;
public
published
  property Columns: TGridColumnCollectionEx read GetColumnsEx write SetColumnsEx;
end;

with

function TAdvColumnGridEx.CreateColumns: TGridColumnCollection;
begin
  Result := TGridColumnCollectionEx.Create(Self);
end;

function TAdvColumnGridEx.GetColumnsEx: TGridColumnCollectionEx;
begin
  Result := TGridColumnCollectionEx(inherited Columns);
end;

procedure TAdvColumnGridEx.SetColumnsEx(
 const Value: TGridColumnCollectionEx);
begin
  TGridColumnCollectionEx(inherited Columns).Assign(Value);
end;

With this inplace, it is now possible to introduce a new property in the TGridColumnItemEx and using this property in the grid. The property introduced here is Hint: string and this property is used to give each column a different hint in TAdvColumnGrid :

TGridColumnItemEx = class(TGridColumnItem)
private
  FHint: string;
public
  procedure Assign(Source: TPersistent); override;
published
  property Hint: string read FHint write FHint;
end;

It is important that the Assign method is also overridden. This takes care that the new properties introduced are also save to file or moved with the columns as these are moved in the grid :

procedure TGridColumnItemEx.Assign(Source: TPersistent);
begin
  inherited;
  FHint := (Source as TGridColumnItemEx).Hint;
end;

Finally, this new hint property is used in the overridden GetCellHint function:

procedure TAdvColumnGridEx.GetCellHint(ACol, ARow: Integer; var AHint: string); begin AHint := TGridColumnItemEx(Columns[ACol]).Hint; end;

With the concepts introduced in this example, you can now fully customize the column properties to enable entirely new uses for TAdvColumnGrid. We look forward to see your creative use of this.

Full source of the TAdvColumnGridEx component can be downloaded here