TAdvStringGrid

Example 29 : Adding a field chooser to TAdvStringGrid

vcl grid

Through 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);
Additionally, the column from where the drag starts is saved in the variable DragColumn.

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);
Finally, after the drag & drop completed succesfully, the column is hidden from the grid and the column header is added to the fieldchooser buttonlistbox. This is done in the OnOleDragStop event :
  FieldChooser.MoveToFieldChooser(DragColumn);
Drag from the fieldchooser - drop on the grid: The ButtonListBox OnOleDragStart, OnOleDragOver and OnOleDragStop events do not much more than make sure that drag & drop within the listbox itself is not allowed and only drop from the grid can be accepted. This is done by just checking the sender of the dragdrop operation.
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.