String Grid with ComboBox Cell Editors

This simple example has the usual fixed row header and fixed column plus only one column of data added. (To add a column, right click on Columns in the Object Inspector then select menu item Add Item). The ButtonStyle property of the editable column is set to cbsPickList. This provides for each editable cell in the column a dropdown list (without the option for the user to type data).

You must remember to set the StringGrid option GoEditing to true in order to see each combo box when you click in its cell. We add three strings (colours) using the Strings editor of the PickList property of the column and another three in code in the FormCreate procedure.

You could use the SaveToFile and LoadFromFile procedures of the StringGrid to save and load the selected strings.

The code of uStringGridCombo.pas and uStringGridCombo.lfm follow a screenshot of a cell editor in use.

Cell Editor in Use

Cell Editor in Use

Code of uStringGridCombo.pas

unit uStringGridCombo;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
  ExtCtrls;

type
  TForm1 = class(TForm)
    shpRect, shpCircle, shpDiamond: TShape;
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1PickListSelect(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Columns[0].PickList.Add('White');
  StringGrid1.Columns[0].PickList.Add('Aqua');
  StringGrid1.Columns[0].PickList.Add('Blue');
  StringGrid1.Cells[0, 0] := 'Shape';
  StringGrid1.Cells[0, 1] := 'Rectangle';
  StringGrid1.Cells[0, 2] := 'Circle';
  StringGrid1.Cells[0, 3] := 'Diamond';
end;

procedure TForm1.StringGrid1PickListSelect(Sender: TObject);
var
  CurrentColour : TColor;
begin
  with StringGrid1 do
    begin
      case Cells[Col, Row] of
        'White': CurrentColour := clWhite;
        'Aqua': CurrentColour := clAqua;
        'Blue': CurrentColour := clBlue;
        'Fuchsia': CurrentColour := clFuchsia;
        'Yellow': CurrentColour := clYellow;
        'Teal': CurrentColour := clTeal;
      end;
      case Row of
        1: shpRect.Brush.Color := CurrentColour;
        2: shpCircle.Brush.Color := CurrentColour;
        3: shpDiamond.Brush.Color := CurrentColour;
      end;
    end;
end;

end.

Code of Form in uStringGridCombo.lfm

object Form1: TForm1
  Left = 266
  Height = 148
  Top = 209
  Width = 143
  Caption = 'Form1'
  ClientHeight = 148
  ClientWidth = 143
  OnCreate = FormCreate
  LCLVersion = '1.2.4.0'
  object StringGrid1: TStringGrid
    Left = 8
    Height = 92
    Top = 48
    Width = 133
    ColCount = 2
    Columns = <  
      item
        ButtonStyle = cbsPickList
        DropDownRows = 3
        PickList.Strings = (
          'Fuschia'
          'Yellow'
          'Teal'
        )
        Title.Caption = 'Colour'
      end>
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing, goSmoothScroll]
    RowCount = 4
    TabOrder = 0
    OnPickListSelect = StringGrid1PickListSelect
  end
  object shpRect: TShape
    Left = 8
    Height = 32
    Top = 8
    Width = 24
  end
  object shpCircle: TShape
    Left = 50
    Height = 29
    Top = 8
    Width = 40
    Shape = stCircle
  end
  object shpDiamond: TShape
    Left = 106
    Height = 29
    Top = 11
    Width = 33
    Shape = stDiamond
  end
end
Programming - a skill for life!

How to use a string grid to display and edit data