TAdvStringGrid

Example 61 : Some ComboBox tricks with TAdvStringGrid

vcl grid

By default, comboboxes are only visible when the inplace editing has started. In some situations, it might be helpful that the user can see through the dropdown image that a cell has a combobox. With TAdvStringGrid this is possible by using one property and one event handler. To enable the display of comboboxes for any cell that has a combobox inplace editor whether the cell is in edit mode or not, set grid.ControlLook.DropDownAlwaysVisible = true. Setting this property is not sufficient as an extra event OnHasComboBox is used for more fine grained control. With OnHasComboBox, some comboboxes can be made always visible and some not.

In this sample application, two columns in the grid use comboboxes. The combobox values in the last column depend on the selected value of the combobox in column 1. In this sample, we added the extra control that the combobox dropdown in column 2 is only visible after a value has been selected in column 1. This was done through:

procedure TForm1.AdvStringGrid1HasComboBox(Sender: TObject; ACol,
  ARow: Integer; var HasComboBox: Boolean);
begin
  HasComboBox := (ACol = 1) or
                 ((ACol = 2) and
                  ((AdvStringGrid1.Cells[1,ARow] <> '') or
                    (AdvStringGrid1.EditMode and (AdvStringGrid1.Row = ARow) and (AdvStringGrid1.Col = 1))));
end;

This event handler causes the combobox dropdown to always display for column 1 and optionally display it for column 2 when the corresponding cell in column 1 has been set or is being edited. In case all combobox dropdown buttons should be visible, this reduces to:

procedure TForm1.AdvStringGrid1HasComboBox(Sender: TObject; ACol,
  ARow: Integer; var HasComboBox: Boolean);
begin
  HasComboBox := True;
end;

Two additional tricks have been used in this sample. To make sure the combobox drops down immediately upon clicking the always visible dropdown button, the property grid.MouseActions.DirectComboDrop was set to true. Finally, to make sure the dropdown in the cell appears immediately in column 2 when a combobox value is selected in column 1, the OnComboChange event is used from where grid.RepaintCell(2,ARow) is called.

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