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;
TGridColumnItemEx = class(TGridColumnItem)
private
FHint: string;
public
procedure Assign(Source: TPersistent); override;
published
property Hint: string read FHint write FHint;
end;
procedure TGridColumnItemEx.Assign(Source: TPersistent);
begin
inherited;
FHint := (Source as TGridColumnItemEx).Hint;
end;
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