TAdvStringGrid

Example 41 : virtual cells and disjunct row selection

vcl grid disjunct row select

The new event, OnGetDisplText opens lots of different new capabilities for using TAdvStringGrid. One of these capabilities is dynamic transformation of grid information at display time. This sample shows 2 possible uses for this approach.

1) Dynamic HTML formatting of grid text

As it is often inconvenient to set text with HTML tags in the grid itself (for later editing / saving etc ...), the OnGetDisplText is an ideal way for setting only the desired text in the grid cell and apply formatting only separately for displaying. In this simple example, text is set bold for the first column by:

procedure TForm1.AdvStringGrid1GetDisplText(Sender: TObject; ACol,ARow: Integer; var Value: string);
begin
  if ACol = 1 then
    Value := '<B>' + Value + '</B>';
end;

The next example makes a more powerful use more clear:

Suppose that a PictureContainer is attached to the grid (See documentation on using

PictureContainers ) with convenient image naming, then we can on the fly convert cell text (that can be edited with a combobox for example) to an image:

procedure TForm1.AdvStringGrid1GetDisplText(Sender: TObject; ACol,ARow: Integer; var Value: string);
begin
  Value := '<IMG SRC="' + Value + '"'>;
end;

2) Dynamic numeric formatting

Suppose that numeric info is stored in the grid cells with a higher precision than required to display. In this case, the data can be reformatted dynamically with a routine such as:

procedure TForm1.AdvStringGrid1GetDisplText(
Sender: TObject; ACol,ARow: Integer; var Value: string);
var
  f: Double;
  Err: Integer;I
begin
  Val(Value,f,Err);
  Value := Format('%.2f',[f]);
end;


Delphi project & source files for downloading included in the main demos distribution for Delphi.