TAdvStringGrid
Example 5 : Combobox editors & varia
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;
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);
TEditorType = (edNormal,edSpinEdit,edComboEdit,edComboList,edEditBtn,edCheckBox,edDateEdit, edTimeEdit,edButton,edDataCheckBox,edNumeric,edFloat,edCapital,edMixedCase);
procedure TForm1.AdvStringGrid1GetEditorType(Sender: TObject; aCol, aRow: Integer; var TEditorType); begin with advstringgrid1 do case acol of 1,2: aEditor:=edComboList; end; end; end;
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;
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.