TAdvStringGrid

Example 5 : Combobox editors & varia

vcl grid editing

Demo 5 shows how to use the combobox editors, especially the use of different combobox lists for different cells. But before delving into the combobox related code, first the grid is made a little more attractive by the use of some images in the column headers. Therefore, an Imagelist control is placed on the form, and 3 bitmaps are added. Further, the EnableGraphics property is set true. Then in the formcreate method, the images are assigned to the column headers cells with:

procedure TForm1.FormCreate(Sender: TObject);
var
  i:integer;
begin
  for i:=0 to 2 do 
  AdvStringGrid1.AddImageIdx(i,0,i,haBeforeText,vaTop);
end; 
Adding the images is easy. Two parameters control the placement of the image relative to the text, ie. horizontally before the text, and vertically aligned to the top of the cell.

Now, let's delve into the combobox related code. In this case, we want the combobox displayed for column 2 to be dependent of the data in the cells in column 1, since the model of the cars is dependent of the brand. To setup this demo very simple, some files have been made : cars.dat, bmw.dat, mercedes.dat, ferrari.dat, audi.dat, vw.dat. The first file contains the names of all brands required in column 1. For each brand, a corresponding file exists with the models. The files are simple ASCII files, with a brand or model on each line. These files can be loaded in a very simple way into stringlist and that's exactly what is needed for this demo. To specify the type of inplace editor, an OnGetEditorType event handler is required. This event handler is called when editing for a specific cells is about to be started. The parameters are :

procedure TForm1.AdvStringGrid1GetEditorType(Sender: TObject; aCol, aRow: Integer; var aEditor: TEditorType); 
It's the task of the program to set the parameter aEditor to either one of these values :

TEditorType = (edNormal,edSpinEdit,edComboEdit,edComboList,edEditBtn,edCheckBox,edDateEdit, 
edTimeEdit,edButton,edDataCheckBox,edNumeric,edFloat,edCapital,edMixedCase);
For this application, a edComboList is used as it's not allowed to specify any other car or type than these from the lists. Secondly, the combobox list must be specified for each column. This is handled in the following way :

procedure TForm1.AdvStringGrid1GetEditorType(Sender: TObject; 
aCol, aRow: Integer; var TEditorType);
begin
    with advstringgrid1 do
    case acol of
    1,2: aEditor:=edComboList;
    end;
   end;
end;
The properties of the inplace-editor, in this case the data of the combobox, need to be set form the OnGetEditorProp event. The implementation of this event becomes:
procedure TForm1.AdvStringGrid1GetEditorProp(Sender: TObject; 
aCol, aRow: Integer; AEditLink: TEditLink);
begin
    with advstringgrid1 do
    case acol of
    1:begin
      ClearComboString;
      if FileExists('cars.dat') then
        Combobox.Items.LoadFromFile('cars.dat');
    end;
    2:begin
      ClearComboString;
      if (cells[1,arow]<>'') then
      if FileExists(cells[1,arow]+'.dat') then
        Combobox.Items.LoadFromFile(cells[1,arow]+'.dat');
    end;
   end;
end;
If the editing is started in column 1, the combobox is filled with data from the file cars.dat. For column 2, the combobox is filled with the data from the file related to the value in the cell in column 1. Notice that Combobox is a property of TAdvStringGrid, so you can access all properties of the combobox such as the Sorted, DropDownCount, Cursor, ItemIndex. This last property ItemIndex can be used to automatically preselect data. For just adding data to the combobox, an additional method of TAdvStringGrid: AddComboString(s:string) is defined.

Some other properties that affect the editing behaviour can be found in Navigation and MouseActions. For example, there is the AlwaysEdit property in Navigation which must be used instead of goAlwaysShowEdit in Options if you always want to have an inplace editor shown. The AdvanceOnEnter can be used to automatically select the next cell when enter is pressed. Furthermore, if AlwaysEdit is not required, but immediate editing is required when clicking with the mouse on the cell, use DirectEdit in MouseActions.

To show one more capability of the grid, lookup editing is turned on by the Lookup property. This means that while editing in the first column, text is automatically completed based on the values in the LookupItems list. This list can dynamically grow while editing if LookupHistory is also turned on. In this example the list is filled with the names of some CEO's of computer companies. If the letter 'P' is entered in the first cell, it will automatically suggest Philippe Khan.

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