The TsWorksheetGrid is a TCustomGrid after all, and thus many of the techniques used for TCustomGrid (or TStringGrid) may work for TsWorksheetGrid, too. The Grids-wiki describes how a different cell editor can be selected (
https://wiki.lazarus.freepascal.org/Grids_Reference_Page#Grid_Cell_Editors) - do the same with the worksheet grid:
procedure TForm1.sWorksheetGrid1SelectEditor(Sender: TObject; aCol,
aRow: Integer; var Editor: TWinControl);
begin
if (aCol = 3) then
begin
Editor := sWorksheetGrid1.EditorByStyle(cbsPickList);
with TPickListCellEditor(Editor) do
begin
Style := csDropDownList;
Items.StrictDelimiter := true;
Items.CommaText := 'Africa,Asia,Europe,North America,Oceania,South America';
end;
end;
end;
- This code is the handler for the grid's OnSelectEditor event.
- The line "Editor := sWorksheetGrid1.EditorByStyle(cbsPickList)" selects a picklist cell editor (combobox) for the cells defined in the preceeding "if" condition (here: all cells of column 3).
- "TPickListCellEditor(Editor).Items.CommaText" defines the items in the combobox as a comma-separated list, here: the earth's continents. Since the items "North America" and "South America" contain spaces we must switch StrictDelimiter to true, otherwise the ComboBox would split these items at the space and add "North", "South" and "America" as separate items. Depending on the leading "if" condition you can use different item lists for specific cells.
- "Style := csDropdownList" disables direct typing for the user.
If you don't like the thick border of the selected cell interfering with the combobox you could additionally call "sWorksheetGrid1.SelectionPen.Style := psClear" in the OnSelectEditor handler - this hides the selection rectangle while the combobox is visible. The counter-part, "sWorksheetGrid1.SelectionPen.Style := psSolid", then should be called in the OnEditingDone event of the grid in order to restore the selection rectangle.