Author: Ernesto De Spirito
The user can move rows and columns of a StringGrid with the mouse. Can it also be
done by code? In the help for TCustomGrid you can see the methods MoveColumn and
MoveRow, but they are hidden in TStringGrid
Answer:
The user can move rows and columns of a StringGrid with the mouse. Can it also be
done by code? In the help for TCustomGrid you can see the methods MoveColumn and
MoveRow, but they are hidden in TStringGrid. We can make them accessible again by
subclassing TStringGrid and declaring these methods as public:
1 type2 TStringGridX = class(TStringGrid)
3 public4 procedure MoveColumn(FromIndex, ToIndex: Longint);
5 procedure MoveRow(FromIndex, ToIndex: Longint);
6 end;
The implementation of these methods simply consists of invoking the corresponding
method of the ancestor:
7 8 procedure TStringGridX.MoveColumn(FromIndex, ToIndex: Integer);
9 begin10 inherited;
11 end;
12 13 procedure TStringGridX.MoveRow(FromIndex, ToIndex: Integer);
14 begin15 inherited;
16 end;
You don't have to register this component in the Components Palette. Use a
TStringGrid or any TCustomGrid descendant, and when you need to call these methods
simply cast the object to the new class. For example:
17 18 procedure TForm1.Button1Click(Sender: TObject);
19 begin20 TStringGridX(StringGrid1).MoveColumn(1, 3);
21 end;
Copyright (c) 2001 Ernesto De Spiritomailto:edspirito@latiumsoftware.com
Visit: http://www.latiumsoftware.com/delphi-newsletter.php