[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] Reference for unit 'Grids' (#lcl)

TCustomStringGrid.Cols

Provides indexed access to the list of values for the specified column number.

Declaration

Source position: grids.pas line 1820

public property TCustomStringGrid.Cols[index: Integer] : TStrings
  read GetCols
  write SetCols;

Description

Cols is an indexed TStrings property which provides access to the list of values for the column number specified in Index. Read access calls MapGetColsRows to retrieve the TStrings instance used as the value for the property.

The Cols implementation in Lazarus is different than the one in Delphi. In Lazarus, a new TStrings instance is created and used to retrieve the cell content. The programmer is responsible for freeing the object after use. Failure to do so will result in a memory leak.

Cast the property value to TStringList to use the features introduced in the descendent class. For example:

Avoid a memory leak when updating column content:

// this will cause a memory leak because the returned TStrings is not freed
Grid.Cols[1].CommaText := '1,2,3,4,5';
Grid.Cols[2].Text := 'a'+#13#10+'s'+#13#10+'d'+#13#10+'f'+#13#10+'g';

// avoids the memory leak from above
Lst := TStringList.Create;
Lst.CommaText := '1,2,3,4,5';
Grid.Cols[1] := Lst;
Lst.Free;

Lst := TStringList.Create;
Grids.Text := 'a'+#13#10+'s'+#13#10+'d'+#13#10+'f'+#13#10+'g';
Grid.Cols[2] := Lst;
Lst.Free;

Store values from a string grid column to a list box:

procedure TForm1.FillListBox1;
var
  StrTempList: TStringList;
begin
  StrTempList := TStringList(Grid.Cols[4]);
  if StrTempList <> nil then
  begin
    ListBox1.Items.Assign(StrTempList);
    StrTempList.Free;
  end;
end;

Version 3.2 Generated 2024-02-25 Home