Example 29 : Adding a field chooser to TAdvStringGridThrough the column drag & drop capabilities built into TAdvStringGrid, it is possible to implement something like a field chooser where columns that should be viewed or not can be dragged into. To achieve this, an OLE drag & drop compatible button listbox has been implemented (It is included in the distribution ZIP file of the example app) Columns can be dragged into the field chooser listbox & vice versa.
To start with, the properties OleDropSource and OleDropTarget have been set to TRUE.
Drag from the grid - drop on the fieldchooser:
The OnOleDrag event is used to allow only drag & drop from the fixed column headers. This is done with :
allow := (arow=0) and (acol>0);
The OnOleDragStart event is used to save the source component of the drag operation to make sure that a drag & drop of a column within the grid itself is not allowed (we only allow drag & drop between the grid and the field chooser) The technique to do this is by checking the sender in the OnOleDragOver event and set Allow to false if the drag source is equal to the drop target. Additionally, the drop can only be allowed on the fixed column headers. Therefore the OnOleDragOver becomes :
allow:=(dragsource=nil) and (acol>0) and (arow=0);
The field is dropped on the grid through the OnOleDropCol event :
procedure TForm1.AdvStringGrid1OleDropCol(Sender: TObject; Arow, Acol, DropCol: Integer); begin FieldChooser.RemoveFromFieldChooser(DropCol,ACol); end;
The OnOleDropCol returns the original column index of the column dropped from the field chooser. (It was saved in the fieldchooser with the AddItem method) First this column that was hidden is displayed again through UnhideColumn. As the UnhideColumn will make the column reappear at the original position, it is moved to the position where it is dropped with the MoveColumn method. Finally, the button associated with the column in the fieldchooser buttonlistbox is removed.
As shown, implementing a field chooser is one of the new exciting capabilities of TAdvStringGrid. At the same time, the drag & drop cell & column implementation should allow other new possibilties as well for your application with a mimimum of additional code.
TButtonListBox for the fieldchooser : btnlistb.zip
Delphi project & source files for downloading included in the main demos distribution for Delphi.