TAdvStringGrid

Example 60 : Using data dependent images with TAdvStringGrid

vcl grid data dependent images

The method grid.AddDataImage(col,row,index,horizAlign,vertAlign) adds an imagelist image to a cell. The index of this imagelist image is dependent of the data in the cell where the image is added. By default, this means that when the cell has the value 0, the first image of the imagelist will be displayed, when the value is 1, the second image will be displayed etc... Not in all circumstances it is convenient that the cell data should be a number. Suppose that we want to display a Yes / No field with textual representation 'Y' and 'N' as two distinct images in the grid from an imagelist.  Normally, this would not be possible, but thanks to the OnGetDisplText, a solution is easily implemented. This OnGetDisplText will convert the values 'Y' and 'N' to the imagelist index values that are needed to display the proper images.  The code snippet that takes care of this is:

procedure TForm1.AdvStringGrid1GetDisplText(Sender: TObject; ACol,
  ARow: Integer; var Value: String);
var
  oldvalue: string;
begin
  oldvalue := value;
  if (ACol = 1) and (ARow > 0) then
  begin
    if value = 'Y' then
      value := '1'
    else
      value := '0';

  end;
end;

With this one event handler, we can set images through:

advstringgrid1.Cells[1,1] := 'N';
advstringgrid1.Cells[1,2] := 'Y';

and just use the text that is most convenient for maintaining in the rest of the application.

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